4. How to order on a field from a related model (with a foreign key)?ΒΆ
You have two models, Category
and Hero
.
class Category(models.Model):
name = models.CharField(max_length=100)
class Hero(models.Model):
# ...
name = models.CharField(max_length=100)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
You want to order Hero
by category and inside each category by the Hero
name. You can do.
Hero.objects.all().order_by(
'category__name', 'name'
)
Note the double underscore(__
) in 'category__name'
. Using the double undertscore, you can order on a field from a related model.
If you look at the SQL.
SELECT "entities_hero"."id",
"entities_hero"."name",
-- more fields
FROM "entities_hero"
INNER JOIN "entities_category" ON ("entities_hero"."category_id" = "entities_category"."id")
ORDER BY "entities_category"."name" ASC,
"entities_hero"."name" ASC