开发者

Ordering by multiplication of several values in Django ORM? [duplicate]

This question already has answers here: Closed 11 years ago.

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'],
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜