Ordering by multiplication of several values in Django ORM? [duplicate]
Possible Duplicate:
Django QuerySet ordering by expression
If I had a model with 3 integer fields, a,b,c, is it possible to开发者_StackOverflow中文版 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'],
)
精彩评论