Internals
Django Deprecation Timeline
Passing callable arguments to querysets will no longer be possible. BaseCommand.requires_model_validation will be removed in favor of requires_system_checks. Admin validators will be replaced by admin checks. The ModelAdmin.validator_class and default_validator_class attributes will be removed. ModelAdmin.validate() will be removed.
4.2
5.0
5.1
Reference
These functions are available from the django.contrib.postgres.aggregates module. They are described in more detail in the PostgreSQL docs. NOTE: All functions come without default aliases, so you must explicitly provide > one.
Reference
PostgreSQL specific model fields
HStoreFieldQuerying HStoreField
To query based on a given key, you can use that key as the lookup name: You can chain other lookups after key lookups: or use F() expressions to annotate a key value.
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
classDateTimeField(auto_now=False,auto_now_add=False,**options)[source] A date and time, represented in Python by a datetime.datetime instance. Takes the same extra arguments as DateField. The default form widget for this field is a single DateTimeInput. The admin uses two separate TextInput widgets with JavaScript shortcuts.
5.1
5.2
6.0
dev
Reference
Model field reference
Field types
classUUIDField(**options)[source] A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL and MariaDB 10.7+, this stores in a uuid datatype, otherwise in a char(32). Universally unique identifiers are a good alternative to AutoField for primary_key.
Reference
Model field reference
Relationship fieldsManyToManyField
ForeignKey.related_name : The name to use for the relation from the related object back to this one. It’s also the default value for related_query_name (the name to use for the reverse filter name from the target model).
5.2
6.0
dev
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
Querying JSONField
To query based on a given dictionary key, use that key as the lookup name: Multiple keys can be chained together to form a path lookup: If the key is an integer, it will be interpreted as an index transform …
6.0
dev
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.):
Index
… - buffer_with_style() (GEOSGeometry method) - build_absolute_uri() (HttpRequest method) - build_suite() (DiscoverRunner method) - built-in function - django.conf.settings.configure() - django.core.cache.utils.make_template_fragment_key() - django.core.management.call_command() - django.core.serializers.get_serializer() - django.views.decorators.cache.cache_page() - bulk_create() (in module django.db.models.query.QuerySet) - bulk_update() (in module django.db.models.query.QuerySet) - byteorder (WKBWriter attribute) |
Index
… 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 …
Index
… (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) - (models.FlatPage method) - get_accessed_time() (Storage method) - get_actions() (ModelAdmin method) - …
Index
… LANGUAGES - setting - languages (Sitemap attribute) - LANGUAGES_BIDI - setting - last - template filter - last() (in module django.db.models.query.QuerySet) - last_attempted_at (TaskResult attribute) - last_login (models.User attribute) - last_modified() (in module django.views.decorators.http) - last_name (models.User attribute) - lastmod …
Index
… NON_FIELD_ERRORS (in module django.core.exceptions) - non_field_errors() (Form method) - NONCE (CSP attribute) - NONE (CSP attribute) - none() (in module django.db.models.query.QuerySet) - noop (RunSQL attribute) - noop() (RunPython static method) - NoReverseMatch - normalize() (GEOSGeometry method) - normalize_email() (models.BaseUserManager class …
Index
… 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 (Index attribute) - (UniqueConstraint attribute) - open() (FieldFile method) - (File method) …
Index
… - value_omitted_from_data() (Widget method) - value_to_string() (Field method) - ValueRange (class in django.db.models.expressions) - values() (backends.base.SessionBase method) - (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) …