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().
743 sections across all versions Narrow to Django 6.0 (current) →
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.
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).
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 …
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 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.
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 (LineString attribute) - (Point attribute) - year - field lookup type - year (YearMixin attribute) - year_format (YearMixin attribute) - YEAR_MONTH_FORMAT - setting | - YearArchiveView (built-in class) - …
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.
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.
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.
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.