[Answer]-Order_by intermediate table for given relation in SQL with Django ORM

1👍

Edit:

This is a Many-to-many relationship. So you can probably leverage that. How about adding default ordering to FoodNutrientAmount and then you can just do normal manytomany queries.

class FoodNutrientAmount(models.Model):
    nutrient = models.ForeignKey(Nutrient)
    food = models.ForeignKey(Food)
    amount = models.FloatField()

    class Meta:
        ordering = ('-amount',)

Then you can just call –

nutritious_foods = Food.objects.filter(nutrients__tagname='FOL').order_by('foodnutrientamount')

Leave a comment