Possible Duplicate:
Django QuerySet ordering by expression
If I had a model with 3 integer fields, a,b,c, is it possible to do an order_by() on the value of abc within the ORM?
Absolutely. Use .extra(select=..., order_by=...)
, using the same name for both.
Yes, although the syntax isn't pretty.
You can do this:
YourModel.objects.extra(
select={'total': 'a * b * c'},
order_by=['total'],
)
http://stackoverflow.com/questions/2926931/django-queryset-ordering-by-expression – amito Aug 28 '11 at 11:26