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.