0

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?

Community
  • 1
  • 1
Matt
  • 101
  • 2
  • 5
  • Answered here:
    http://stackoverflow.com/questions/2926931/django-queryset-ordering-by-expression
    – amito Aug 28 '11 at 11:26

2 Answers2

1

Absolutely. Use .extra(select=..., order_by=...), using the same name for both.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Yes, although the syntax isn't pretty.

You can do this:

YourModel.objects.extra(
    select={'total': 'a * b * c'},
    order_by=['total'],
)
Wolph
  • 78,177
  • 11
  • 137
  • 148