djangodocs.org

766 sections in Django dev Search all versions →

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

No deferred queries

Topic guide Making queries Asynchronous queries

Relations are deferred by default. If the blog relation is not prefetched, accessing entry.blog during asynchronous evaluation of a queryset attempts a blocking database query, raising SynchronousOnlyOperation.

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.

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.

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.

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

Arguments

Topic guide Django shortcut functions get_list_or_404()

klass A Model, Manager or QuerySet instance from which to get the list. *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.

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

Internals Django Deprecation Timeline

django.core.paginator.QuerySetPaginator will be removed. A model’s Meta.ordering will no longer affect GROUP BY queries. django.contrib.postgres.fields.FloatRangeField and django.contrib.postgres.forms.FloatRangeField will be removed. The FILE_CHARSET setting will be removed. django.contrib.staticfiles.storage.CachedStaticFilesStorage will be removed. RemoteUserBackend.configure_user() will require request as the first positional argument.

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.

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.

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.

StringAgg

Reference PostgreSQL specific aggregation functions General-purpose aggregation functions

classStringAgg(expression,delimiter,distinct=False,filter=None,default=None,order_by=()) : > WARNING: > > Deprecated since version 6.0: The PostgreSQL StringAgg class is deprecated in favor of the > generally available StringAgg class.