I have found a few questions that are similar to mine, but most of them are dated, or too (or too little) verbose to be helpful.
I have a model like this:
class Breakfast(models.Model):
count_eggs = models.IntegerField()
count_bacon = models.IntegerField()
had_toast = models.BooleanField()
Now, in building a RESTful API, I need the ability to sort Breakfast objects by the total of count_eggs + count_bacon
without storing this permanently on the model.
Many of the current and popular questions suggest something like this:
Breakfast.objects.extra(
select={'total_food':'count_eggs + count_bacon'},
order_by=('total_food',)
)
This seems to work for many, but Django docs appear to dissuade this solution. So, in the 1.10+ world, What is the best/correct way to do this type of filtering on the sum of two (or more) fields in Django
order_by
, but I suspect there is a better answer.