djangodocs.org

1413 sections across all versions Narrow to Django 6.0 (current) →

Example

Topic guide Django shortcut functions get_list_or_404()

However, you can also pass a QuerySet instance: The above example is a bit contrived since it’s equivalent to doing: but it can be useful if you are passed the queryset variable from somewhere else.

6.0

Model managers

Topic guide Migrations Migration files

… in RunPython operations. This is done by defining a use_in_migrations attribute on the manager class: If you are using the from_queryset() function to dynamically generate a manager class, you need to inherit from the generated class to make it importable: …

4.2 5.0 5.1 5.2 6.0

TransactionTestCase

Topic guide Testing tools Provided test case classes

classTransactionTestCase[source] TransactionTestCase inherits from SimpleTestCase to add some database-specific features: Resetting the database to a known state at the end of each test to ease testing and using the ORM. Database fixtures. Test skipping based on database backend features.

4.2 5.0 5.1 5.2 6.0

Assertions

Topic guide Testing tools Test cases features

See assertJSONEqual() for further details. TransactionTestCase.assertQuerySetEqual(qs,values,transform=None,ordered=True,msg=None)[source] : Asserts that a queryset qs matches a particular iterable of values values. TransactionTestCase.assertNumQueries(num,func,*args,**kwargs)[source] : Asserts that when func is called with *args and **kwargs that num database queries are executed.

4.2 5.0 5.1 5.2 6.0

6.0

Internals Django Deprecation Timeline

The Prefetch.get_current_queryset() method will be removed. The get_prefetch_queryset() method of related managers and descriptors will be removed. get_prefetcher() and prefetch_related_objects() will no longer fallback to get_prefetch_queryset(). The FORMS_URLFIELD_ASSUME_HTTPS transitional setting will be removed.

4.2 5.2

5.0

Internals Django Deprecation Timeline

Support for pytz timezones will be removed. The is_dst argument will be removed from: QuerySet.datetimes() django.utils.timezone.make_aware() django.db.models.functions.Trunc() django.db.models.functions.TruncSecond() django.db.models.functions.TruncMinute() django.db.models.functions.TruncHour() django.db.models.functions.TruncDay() django.db.models.functions.TruncWeek() django.db.models.functions.TruncMonth() django.db.models.functions.TruncQuarter() django.db.models.functions.TruncYear() The django.contrib.gis.admin.GeoModelAdmin and OSMGeoAdmin classes will be removed. The undocumented BaseForm._html_output() method will be removed.

4.2 5.0 5.1 5.2

1.9

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.

5.0 5.1 5.2 6.0

6.0

Internals Django Deprecation Timeline

The Prefetch.get_current_queryset() method will be removed. The get_prefetch_queryset() method of related managers and descriptors will be removed. get_prefetcher() and prefetch_related_objects() will no longer fallback to get_prefetch_queryset(). The FORMS_URLFIELD_ASSUME_HTTPS transitional setting will be removed.

5.0 5.1

1.9

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 5.2

TextField limitations

Reference Databases Oracle notes

LOB columns may not be used in a SELECT DISTINCT list. This means that attempting to use the QuerySet.distinct method on a model that includes TextField columns will result in an ORA-00932 error when run against Oracle.

4.2 5.0 5.1

overlap

Reference PostgreSQL specific model fields Range FieldsQuerying Range FieldsContainment functions

Returns objects where the data shares any results with the values passed. Uses the SQL operator &&. For example: Changed in Django 4.2: Support for QuerySet.values() and values_list() as a right-hand > side was added.

4.2 5.0

1.9

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

1.9

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.

dev

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.

dev

Key lookups

Reference PostgreSQL specific model fields

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.

dev

Database Functions

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.

dev

Some examples

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

dev

DateTimeField

Reference Model field reference

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.

dev

UUIDField

Reference Model field reference

classUUIDField(**options)[source] A field for storing universally unique identifiers. Uses Python’s UUID class. When used on PostgreSQL and MariaDB, this stores in a uuid datatype, otherwise in a char(32). Universally unique identifiers are a good alternative to AutoField for primary_key.

dev