Reference
The classes documented below provide a way for users to use functions provided by the underlying database as annotations, aggregations, or filters in Django.
674 sections in Django 4.2 Search all versions →
Reference
The classes documented below provide a way for users to use functions provided by the underlying database as annotations, aggregations, or filters in Django.
Reference Query Expressions
Both the querysets below are equivalent. >>> Company.objects.filter(num_employees__gt=F("num_chairs") * 2) >>> Company.objects.filter(num_employees__gt=F("num_chairs") + F("num_chairs")) # How many chairs are needed for each company to seat all employees? >>> company = ( ... Company.objects.filter(num_employees__gt=F("num_chairs")) ... .annotate(chairs_needed=F("num_employees") - F("num_chairs")) ... .first() ...
Reference Model field reference Field types
classUUIDField(**options) A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL, this stores in a uuid datatype, otherwise in a char(32). Universally unique identifiers are a good alternative to AutoField for primary_key.
Reference The Django template language
… relationship to a model called “task” you can loop through all comments attached to a given task like this: Similarly, QuerySets provide a count() method to count the number of objects they contain. Therefore, you can obtain a count of …
Topic guide Using mixins with class-based views
For example, a JSON mixin might look something like this: NOTE: Check out the Serializing Django objects documentation for more > information on how to correctly transform Django models and querysets into > JSON.
Topic guide Making queries Related objectsOne-to-many relationships
This Manager returns QuerySets, which can be filtered and manipulated as described in the “Retrieving objects” section above. Example: You can override the FOO_set name by setting the related_name parameter in the ForeignKey definition.
Topic guide Search Use Cases
Text-based fields have a selection of matching operations.
Topic guide Creating forms from models ModelForm
Consider this set of models: With these models, the ModelForm subclasses above would be roughly equivalent to this (the only difference being the save() method, which we’ll discuss in a moment.):
… in django.forms) - DateMixin (class in django.views.generic.dates) - DateRangeField (class in django.contrib.postgres.fields) - (class in django.contrib.postgres.forms) - dates() (in module django.db.models.query.QuerySet) - DATETIME_FORMAT - setting - DATETIME_INPUT_FORMATS - setting - DateTimeField (class in django.db.models) - (class in django.forms) - DateTimeInput …
… (class in django.core.files.storage) - FileUploadHandler (class in django.core.files.uploadhandler) - filter - template tag - filter() (django.template.Library method) - (in module django.db.models.query.QuerySet) - filter_horizontal (ModelAdmin attribute) - filter_vertical (ModelAdmin attribute) - filterable (Expression attribute) - FilteredRelation (class in django.db.models) - final_catch_all_view …
… (django.views.generic.detail.BaseDetailView method) - (django.views.generic.edit.BaseCreateView method) - (django.views.generic.edit.BaseUpdateView method) - (django.views.generic.edit.ProcessFormView method) - (django.views.generic.list.BaseListView method) - (HttpResponse method) - (in module django.db.models.query.QuerySet) - (QueryDict method) - get_absolute_url() (Model method) - get_accessed_time() (Storage method) - get_actions() (ModelAdmin method) - get_all_permissions() (BaseBackend method) …
… LANGUAGES - setting - languages (Sitemap attribute) - LANGUAGES_BIDI - setting - last - template filter - last() (in module django.db.models.query.QuerySet) - last_login (models.User attribute) - last_modified() (in module django.views.decorators.http) - last_name (models.User attribute) - lastmod (Sitemap attribute) - LastValue …
… attribute) - non_atomic_requests() (in module django.db.transaction) | - NON_FIELD_ERRORS (in module django.core.exceptions) - non_field_errors() (Form method) - none() (in module django.db.models.query.QuerySet) - noop (RunSQL attribute) - noop() (RunPython static method) - NoReverseMatch - normalize() (GEOSGeometry method) - normalize_email() (models.BaseUserManager class …
… on_delete (ForeignKey attribute) - one_to_many (Field attribute) - one_to_one (Field attribute) - OneToOneField (class in django.db.models) - only() (in module django.db.models.query.QuerySet) - OpClass (class in django.contrib.postgres.indexes) - opclasses (ExclusionConstraint attribute) - (Index attribute) - (UniqueConstraint attribute) - open() (FieldFile method) …
… (Field method) - value_omitted_from_data() (Widget method) - value_to_string() (Field method) - ValueRange (class in django.db.models.expressions) | - values() (in module django.db.models.query.QuerySet) - (QueryDict method) - values_list() (in module django.db.models.query.QuerySet) - Variance (class in django.db.models) - vary_on_cookie() (in module django.views.decorators.vary) - …
queryset : An object representing some set of rows to be fetched from the database. slug : A short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs.
How-to How to create custom model fields Writing a field subclassUseful methods
The methods are also called when constructing a WHERE clause that includes the model field – that is, when you retrieve data using QuerySet methods like get(), filter(), and exclude() and have the model field as an argument.
Internals Django Deprecation Timeline
See the Django 1.5 release notes for more details on these changes. The module django.utils.simplejson will be removed. The standard library provides json which should be used instead. The function django.utils.itercompat.product will be removed.
Internals The Django source code repository Tags
queryset-refactor: A refactoring of the internals of Django’s object-relational mapper. This became part of Django as of the 1.0 release. unicode: A refactoring of Django’s internals to consistently use Unicode-based strings in most places within Django and Django applications.
Reference Class-based generic views - flattened index Date-based views
… accessor): allow_empty [get_allow_empty()] allow_future [get_allow_future()] content_type context_object_name [get_context_object_name()] date_field [get_date_field()] extra_context http_method_names model ordering [get_ordering()] paginate_by [get_paginate_by()] paginate_orphans [get_paginate_orphans()] paginator_class queryset [get_queryset()] response_class [render_to_response()] template_engine template_name [get_template_names()] template_name_suffix Methods as_view() dispatch() get() get_context_data() get_date_list() get_dated_items() get_dated_queryset() get_paginator() head() http_method_not_allowed() paginate_queryset() …