djangodocs.org

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

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

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.

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.

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.

Serializing data

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.

SQL efficiency

Design philosophies Database API

This is also why the select_related() QuerySet method exists. It’s an optional performance booster for the common case of selecting “every related object.”

Reference django.contrib.auth Authentication backends

[`with_perm()`](#django.contrib.auth.backends.ModelBackend.with_perm) also allows an object to be passed as a parameter, but unlike other methods it returns an empty queryset if `obj is not None`. `authenticate(request,username=None,password=None,**kwargs)`[[source]](https://github.com/django/django/blob/stable/6.0.x/django/contrib/auth/backends.py#L59) `aauthenticate(request,username=None,password=None,**kwargs)` : *Asynchronous version*: `aauthenticate()` Tries to authenticate `username` with `password` by calling [`User.check_password`](#django.contrib.auth.models.User.check_password).

Func() expressions

Reference Query Expressions Built-in Expressions

… directly: or they can be used to build a library of database functions: But both cases will result in a queryset where each model is annotated with an extra attribute field_lower produced, roughly, from the following SQL: See Database Functions …

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:

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.

Paginator class

Reference Paginator

Paginator.object_list : Required. A list, tuple, QuerySet, or other sliceable object with a count() or __len__() method. For consistent pagination, QuerySets should be ordered, e.g. with an order_by() clause or with a default ordering on the model. Paginator.per_page : Required.

Y

Index

| | | | --- | --- | | - y (LineString attribute) - (Point attribute) - year - field lookup type - year (YearMixin attribute) - year_format (YearMixin attribute) - YEAR_MONTH_FORMAT - setting | - YearArchiveView (built-in class) - …

3.0

Internals Django Deprecation Timeline

The field_name keyword argument of QuerySet.earliest() and latest() will be removed. See the Django 2.1 release notes for more details on these changes. django.contrib.gis.db.models.functions.ForceRHR will be removed. django.utils.http.cookie_date() will be removed.

6.0 dev

BitAnd

Reference PostgreSQL specific aggregation functions General-purpose aggregation functions

classBitAnd(expression,filter=None,default=None,**extra) : Returns an int of the bitwise AND of all non-null input values, or default if all values are null.

dev

BitOr

Reference PostgreSQL specific aggregation functions General-purpose aggregation functions

classBitOr(expression,filter=None,default=None,**extra) : Returns an int of the bitwise OR of all non-null input values, or default if all values are null.

dev

BitXor

Reference PostgreSQL specific aggregation functions General-purpose aggregation functions

classBitXor(expression,filter=None,default=None,**extra) : Returns an int of the bitwise XOR of all non-null input values, or default if all values are null.

dev