djangodocs.org

1413 sections across all versions Narrow to Django 6.0 (current) →

Update in bulk

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.

4.2 5.0 5.1 5.2 6.0

Retrieving all objects

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.

4.2 5.0 5.1 5.2 6.0

Field lookups

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).

4.2 5.0 5.1 5.2 6.0

Raw SQL fragments

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.

6.0

Deferring model fields

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.

4.2 5.0 5.1 5.2 6.0

Field types

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.

5.2 6.0

Arguments

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().

4.2 5.0 5.1 5.2 6.0

The Paginator class

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.

4.2 5.0 5.1 5.2 6.0

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.

4.2 5.0 5.1 5.2 6.0

SQL injection protection

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.

4.2 5.0 5.1 5.2 6.0

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.

5.1 5.2

Arguments

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().

4.2 5.0 5.1 5.2

Field types

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.

4.2 5.0 5.1

The action decorator

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:

4.2 5.0 5.1 5.2 6.0

Raw SQL expressions

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.

4.2 5.0 5.1 5.2 6.0