Order By Django.How

Author avatar wrote on 06/06/2022

Regular

Author.objects.order_by('name')

Related Model

Book.objects.order_by('author__name')

Reverse

Author.objects.order_by('-name')

Multiple Fields

Author.objects.order_by('-name', 'city')

Random Ordering

Author.objects.order_by('?')

Case Sensitive

Author.objects.order_by(Lower('name'))

No Ordering

Author.objects.order_by()

Overriding Order By

Author.objects.order_by('name').order_by('city')

Reverse

queryset = my_queryset.reverse()[:5]

Distinct

queryset = Author.objects.distinct()

CRUDgen Django CRUD generator