Django ORM Cookbook¶
Django ORM Cookbook is a book about doing things with Django ORM and Django models.
Django is a “MTV” (Model-Template-View) framework – This book provides a deep dive into the M
part.
They take the form of about 50 questions of the form How to do X with Django ORM/Queryset/Models
.
Querying and Filtering¶
- 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?
Creating, Updating and Deleting things¶
- 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?
Ordering things¶
Database Modelling¶
- 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 ?