djangodocs.org

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

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.

dev

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.

dev

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.

dev

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.

dev

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

4.2 5.0 5.1 5.2 6.0

ListView

Reference Generic display views

classdjango.views.generic.list.ListView : A page representing a list of objects. classdjango.views.generic.list.BaseListView : A base view for displaying a list of objects.

4.2 5.0 5.1 5.2 6.0

Reference Admin actions Advanced action techniques

Actions may limit their availability to users with specific permissions by wrapping the action function with the action() decorator and passing the permissions argument: The make_published() action will only be available to users that pass the ModelAdmin.has_change_permission() check.

4.2 5.0 5.1 5.2 6.0

Using a SimpleListFilter

Reference ModelAdmin List Filters

… own list filter by subclassing django.contrib.admin.SimpleListFilter. You need to provide the title and parameter_name attributes, and override the lookups and queryset methods, e.g.: NOTE: As a convenience, the HttpRequest object is passed to the lookups > and queryset methods, for …

4.2 5.0 5.1 5.2 6.0

ModelAdmin methods

Reference The Django admin site ModelAdmin objects

ModelAdmin.formfield_for_manytomany(db_field,request,**kwargs) : Like the formfield_for_foreignkey method, the formfield_for_manytomany method can be overridden to change the default formfield for a many to many field.

4.2 5.0 5.1 5.2 6.0

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

4.2 5.0 5.1 5.2 6.0

GeometryDistance

Reference Geographic Database Functions Measurements

classGeometryDistance(expr1,expr2,**extra)[source] Availability: PostGIS Accepts two geographic fields or expressions and returns the distance between them. When used in an order_by() clause, it provides index-assisted nearest-neighbor result sets.

4.2 5.0 5.1 5.2 6.0

Shortcuts

Reference The sitemap framework

The sitemap framework provides a convenience class for a common case: classGenericSitemap(info_dict,priority=None,changefreq=None,protocol=None)[source] : The django.contrib.sitemaps.GenericSitemap class allows you to create a sitemap by passing it a dictionary which has to contain at least a queryset entry.

4.2 5.0 5.1 5.2 6.0

ObjectDoesNotExist

Reference Django Exceptions Django Core Exceptions

exceptionObjectDoesNotExist[source] : The base class for Model.DoesNotExist exceptions. A try/except for ObjectDoesNotExist will catch DoesNotExist exceptions for all models.

4.2 5.0 5.1 5.2 6.0

MultipleObjectsReturned

Reference Django Exceptions Django Core Exceptions

exceptionMultipleObjectsReturned[source] : The base class for Model.MultipleObjectsReturned exceptions. A try/except for MultipleObjectsReturned will catch MultipleObjectsReturned exceptions for all models.

4.2 5.0 5.1 5.2 6.0

Reference Form fields

Two fields are available for representing relationships between models: ModelChoiceField and ModelMultipleChoiceField. Both of these fields require a single queryset parameter that is used to create the choices for the field.

4.2 5.0 5.1 5.2 6.0