How can I tell the Django ORM to reverse the order of query results?


Question

In my quest to understand queries against Django models, I've been trying to get the last 3 added valid Avatar models with a query like:

newUserAv = Avatar.objects.filter(valid=True).order_by("date")[:3]

However, this instead gives me the first three avatars added ordered by date. I'm sure this is simple, but I've had trouble finding it in the Django docs: how do I select the last three avatar objects instead of the first three?

1
43
4/7/2015 6:20:45 PM

Accepted Answer

Put a hyphen before the field name.

.order_by('-date')
104
9/24/2009 9:40:23 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon