django-queryset-6

Last Updated :
Discuss
Comments

For a view that first orders posts by date in ascending order, which method reverses that ordering without rewriting the order_by call, and why is using -date directly often better for large datasets?

Post.objects.filter(published=True).reverse('date') which treats the argument as a lookup and is invalid

Post.objects.order_by('date').exclude(reverse=True) which does nothing related to reversing

Post.objects.reverse().order_by('date') which reverses before ordering and produces inefficient, unpredictable results

Post.objects.order_by('date').reverse() which flips the order and using -date directly is faster because the database can sort once without an extra reversal step

Tags:
Share your thoughts in the comments