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.
1413 sections across all versions Narrow to Django 6.0 (current) →
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
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.
Tutorial Writing your first Django app, part 3 Raising a 404 error
It’s a very common idiom to use get() and raise Http404 if the object doesn’t exist. Django provides a shortcut.
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 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.
Reference Date-based mixins
classDateMixin[source] : A mixin class providing common behavior for all date-based views.
Reference Single object mixins
classdjango.views.generic.detail.SingleObjectMixin : Provides a mechanism for looking up an object associated with the current HTTP request.
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.
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 …
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.
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).
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.
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.
Reference The sitemap framework Shortcuts
Here’s an example of a URLconf using GenericSitemap:
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.
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.
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.