Topic guide Database access optimization Don’t retrieve things you don’t need
If you are going to need other data from the QuerySet, evaluate it immediately.
1413 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Database access optimization Don’t retrieve things you don’t need
If you are going to need other data from the QuerySet, evaluate it immediately.
Topic guide Database access optimization Use bulk methods
When creating objects, where possible, use the bulk_create() method to reduce the number of SQL queries.
Topic guide Database access optimization Use bulk methods
Given a list or queryset of objects: The following example: …is preferable to: Note that there are a number of caveats to this method, so make sure it’s appropriate for your use case.
Topic guide Making queries Retrieving objects
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.
Topic guide Making queries Retrieving objects
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).
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.
Topic guide Performing raw SQL queries Performing raw 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.
Topic guide Creating forms from models ModelForm
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.
Topic guide Django shortcut functions get_list_or_404()
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().
Topic guide Pagination
Under the hood, all methods of pagination use the Paginator class. It does all the heavy lifting of actually splitting a QuerySet into Page objects.
Topic guide Pagination
Here’s an example using Paginator in a view function to paginate a queryset: In the template list.html, you can include navigation between pages in the same way as in the template for the ListView above.
Topic guide Performance and optimization Understanding laziness
Django is itself quite lazy. A good example of this can be found in the evaluation of a QuerySet. QuerySets are lazy.
Topic guide Security in Django
This can result in records being deleted or data leakage. Django’s querysets are protected from SQL injection since their queries are constructed using query parameterization. A query’s SQL code is defined separately from the query’s parameters.
Topic guide Serializing Django objects
At the highest level, you can serialize data like this: The arguments to the serialize function are the format to serialize the data to (see Serialization formats) and a QuerySet to serialize.
Reference Model instance reference
If you delete a field from a model instance, accessing it again reloads the value from the database: Model.refresh_from_db(using=None,fields=None,from_queryset=None)[source] Model.arefresh_from_db(using=None,fields=None,from_queryset=None) Asynchronous version: arefresh_from_db() If you need to reload a model’s values from the database, you can use the refresh_from_db() method.
Topic guide Django shortcut functions get_list_or_404()
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().
Topic guide Creating forms from models ModelForm
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.
Reference Admin actions
action(*,permissions=None,description=None)[source] : This decorator can be used for setting specific attributes on custom action functions that can be used with actions:
Reference The sitemap framework
classSitemap[source] : A Sitemap class can define the following methods/attributes:
Reference Query Expressions Built-in Expressions
classRawSQL(sql,params,output_field=None)[source] Sometimes database expressions can’t easily express a complex WHERE clause. In these edge cases, use the RawSQL expression.