티스토리 뷰

728x90
# reverse
queryset1 = reversed(Collection.objects.filter(pk__in = li))
queryset2 = Collection.objects.filter(pk__in = li).reverse()
queryset3 = Collection.objects.filter(pk__in = li)[::-1]


# desc
queryset = Collection.objects.filter(pk__in = li).order_by('-id')


# limit
queryset = Collection.objects.filter(pk__in = li)[:10]

https://stackoverflow.com/questions/31303327/reverse-queryset-order-in-django

 

Reverse Queryset Order in Django

Is there any simple way to reverse the order of a queryset in Django? Example: li = [1, 2, 3] queryset = Collection.objects.filter(pk__in=li)

stackoverflow.com

https://stackoverflow.com/questions/6574003/django-limiting-query-results

 

Django - limiting query results

I want to take the last 10 instances of a model and have this code: Model.objects.all().order_by('-id')[:10] Is it true that firstly pick up all instances, and then take only 10 last ones? Is th...

stackoverflow.com

https://stackoverflow.com/questions/5876998/reversing-a-list-using-slice-notation

 

Reversing a list using slice notation

in the following example: foo = ['red', 'white', 'blue', 1, 2, 3] where: foo[0:6:1] will print all elements in foo. However, foo[6:0:-1] will omit the 1st or 0th element. >>> foo[6:0:-1...

stackoverflow.com

https://django-orm-cookbook-ko.readthedocs.io/en/latest/asc_or_desc.html

 

1. 쿼리셋을 오름차순/내림차순으로 정렬할 수 있나요? — Django ORM Cookbook 2.0 documentation

1. 쿼리셋을 오름차순/내림차순으로 정렬할 수 있나요? order_by 메서드로 쿼리셋을 정렬할 수 있습니다. 기준 필드를 지정해 오름차순 혹은 내림차순으로 정렬할 수 있습니다. 다음 코드를 살펴보

django-orm-cookbook-ko.readthedocs.io

 

728x90
댓글