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
Рецепты Django ORM 2.0
Contents
- 1. How to find the query associated with a queryset?
- 2. How to do OR queries in Django ORM?
- 3. How to do AND queries in Django ORM?
- 4. How to do a NOT query in Django queryset?
- 5. How to do union of two querysets from same or different models?
- 6. How to select some fields only in a queryset?
- 7. How to do a subquery expression in Django?
- 8. How to filter a queryset with criteria based on comparing their field values
- 9. How to filter FileField without any file?
- 10. How to perform join operations in django ORM?
- 11. How to find second largest record using Django ORM ?
- 12. Find rows which have duplicate field values
- 13. How to find distinct field values from queryset?
- 14. How to use
Q
objects for complex queries? - 15. How to group records in Django ORM?
- 16. How to efficiently select a random object from a model?
- 17. How to use arbitrary database functions in querysets?
- 1. How to create multiple objects in one shot?
- 2. How to copy or clone an existing model object?
- 3. How to ensure that only one object can be created?
- 4. How to update denormalized fields in other models on save?
- 5. How to perform truncate like operation using Django ORM?
- 6. What signals are raised by Django during object creation or update?
- 7. How to convert string to datetime and store in database?
- 1. How to order a queryset in ascending or descending order?
- 2. How to order a queryset in case insensitive manner?
- 3. How to order on two fields
- 4. How to order on a field from a related model (with a foreign key)?
- 5. How to order on an annotated field?
- 1. How to model one to one relationships?
- 2. How to model one to many relationships?
- 3. How to model many to many relationships?
- 4. How to include a self-referencing ForeignKey in a model
- 5. How to convert existing databases to Django models?
- 6. How to add a model for a database view?
- 7. How to create a generic model which can be related to any kind of entity? (Eg. a Category or a Comment?)
- 8. How to specify the table name for a model?
- 9. How to specify the column name for model field?
- 10. What is the difference between
null=True
andblank=True
? - 11. How to use a UUID instead of ID as primary key?
- 12. How to use slug field with django for more readability?
- 13. How to add multiple databases to the django application ?
- 1. How to assert that a function used a fixed number of queries?
- 2. How to speed tests by reusing database between test runs?
- 3. How to reload a model object from the database?
Extra
You are here:
-
Documentation Django Рецепты Django ORM 2.0
- 4. How to order on a field from a related model (with a foreign key)?