Topic guide
Using mixins with class-based views
Lists of objects follow roughly the same pattern: we need a (possibly paginated) list of objects, typically a QuerySet, and then we need to make a TemplateResponse with a suitable template using that list of objects.
dev
Topic guide
Aggregation
When developing a complex query that involves both annotate() and filter() clauses, pay particular attention to the order in which the clauses are applied to the QuerySet.
dev
Topic guide
Aggregation
For example, to order a QuerySet of books by the number of authors that have contributed to the book, you could use the following query:
dev
Topic guide
Fetch modes
Set the default fetch mode for a model class with a custom manager that overrides get_queryset():
dev
Topic guide
Managers
There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.
dev
Topic guide
Database access optimization
Use QuerySet.explain() to understand how specific QuerySets are executed by your database. You may also want to use an external project like django-debug-toolbar, or a tool that monitors your database directly.
dev
Topic guide
Database access optimization
For instance: At the most basic level, use filter and exclude to do filtering in the database. Use F() expressions to filter based on other fields within the same model. Use annotate to do aggregation in the database.
dev
Topic guide
Database access optimization
There are two reasons to use a column with unique or db_index when using get() to retrieve individual objects. First, the query will be quicker because of the underlying database index.
dev
Topic guide
Database access optimization
If you are going to need other data from the QuerySet, evaluate it immediately.
dev
Topic guide
Database access optimization
When creating objects, where possible, use the bulk_create() method to reduce the number of SQL queries. For example: …is preferable to: Note that bulk_create() has several caveats, so ensure it’s appropriate for your use case.
dev
Topic guide
Database access optimization
Given a list or queryset of objects: The following example: …is preferable to: Note that bulk_update() has several caveats, so ensure it’s appropriate for your use case.
dev
Topic guide
Making queries
The simplest way to retrieve objects from a table is to get all of them. To do this, use the all() method on a Manager: The all() method returns a QuerySet of all the objects in the database.
dev
Topic guide
Making queries
Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).
dev
Topic guide
Making queries
Relations are deferred by default. If the blog relation is not prefetched, accessing entry.blog during asynchronous evaluation of a queryset attempts a blocking database query, raising SynchronousOnlyOperation.
dev
Topic guide
Performing raw SQL queries
In some cases, you may need to embed raw SQL fragments directly into ORM queries — for example, in annotate() or filter() calls. Use Func() expressions for calling database functions across backends, or RawSQL for arbitrary parameterized SQL fragments.
dev
Topic guide
Performing raw SQL queries
Fields may also be left out: The Person objects returned by this query will be deferred model instances (see defer()). This means that the fields that are omitted from the query will be loaded on demand.
dev
Topic guide
Creating forms from models
As you might expect, the ForeignKey and ManyToManyField model field types are special cases: ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
dev
Topic guide
Django shortcut functions
get_object_or_404(klass,*args,**kwargs)[source] aget_object_or_404(klass,*args,**kwargs) : Asynchronous version: aget_object_or_404()
dev
Topic guide
Django shortcut functions
klass A Model class, a Manager, or a QuerySet instance from which to get the object. *args Q() objects. **kwargs Lookup parameters, which should be in the format accepted by get() and filter().
dev
Topic guide
Django shortcut functions
get_list_or_404(klass,*args,**kwargs)[source] aget_list_or_404(klass,*args,**kwargs) : Asynchronous version: aget_list_or_404()
dev