开发者

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

开发者 https://www.devze.com 2023-03-31 02:03 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Django QuerySet ordering by expression
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

精彩评论

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