-1

I want to order queryset by sum of two fields.
Essentially, I want to change the default sort order of a model like this:(which doesn't work)

class Meta:
   ordering = ('-my_property_but_not_field', 'my_another_field')

my_property_but_not_field will sum up two fields of the model.

I've seen solutions which use:

extra : Django order_by sum of fields
annotate : Order a QuerySet by aggregate field value
manager : Custom ordering in Django

With extra and annotate, I'll have to change every code which needs the new sorting order.

With manager, I don't have such a problem.
But I don't know how to emulate the order_by_1, order_by_2.

Community
  • 1
  • 1
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Your code does not match your description. That does not either a) order by the sum of those fields or b) even work, since you can't use a non-field in an order call. – Daniel Roseman Jun 12 '13 at 08:44
  • @Daniel: Yes that's the question, I can't use the non-field in `ordering` but how can I achieve the same result using other method? I edited the question a bit to clarify. – eugene Jun 12 '13 at 08:46

1 Answers1

0

If it suits your usecase, I would simply add the sum as a new field and update it on the model's save() and get rid of the property.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • ah that's very doable.. silly me.. although I think original question could wait for another answer – eugene Jun 12 '13 at 09:04