Topic guide
Making queries
Querying JSONFieldKey, index, and path transforms
classKT(lookup) : Represents the text value of a key, index, or path transform of JSONField. You can use the double underscore notation in lookup to chain dictionary key and index transforms.
Topic guide
Making queries
Example: You can also delete objects in bulk. Every QuerySet has a delete() method, which deletes all members of that QuerySet.
Topic guide
Making queries
These accessors may be prefetched by the QuerySet methods select_related() or prefetch_related(). If not prefetched, access will trigger an on-demand fetch through the model’s fetch mode.
dev
Topic guide
Making queries
Related objectsOne-to-many relationships
This Manager returns QuerySet instances, 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
Making queries
Related objectsOne-to-many relationships
If you would like to specify a different manager for a given query you can use the following syntax: If EntryManager performed default filtering in its get_queryset() method, that filtering would apply to the all() call.
Topic guide
For example: The QuerySet API is extensive. You can annotate and aggregate using many built-in database functions. Beyond those, you can create > custom query expressions. Before using raw SQL, explore the ORM.
Topic guide
Performing raw SQL queries
The raw() manager method can be used to perform raw SQL queries that return model instances: Manager.raw(raw_query,params=(),translations=None) This method takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance.
Topic guide
Creating forms from models
Model formsets
As with regular formsets, you can use the max_num and extra parameters to modelformset_factory() to limit the number of extra forms displayed.
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
dev
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: …
Topic guide
Performance and optimization
General approachesGet things right from the start
For example, you might find that you could calculate the same thing - the number of items in a collection, perhaps - in a QuerySet, in Python, or in a template.
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.
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.0
5.1
6.0
Internals
Django Deprecation Timeline
Remove the backward compatible shims introduced to rename get_query_set and similar queryset methods. This affects the following classes: BaseModelAdmin, ChangeList, BaseCommentNode, GenericForeignKey, Manager, SingleRelatedObjectDescriptor and ReverseSingleRelatedObjectDescriptor. Remove the backward compatible shims introduced to rename the attributes ChangeList.root_query_set and ChangeList.query_set.
5.2
6.0
dev
Reference
Admin actions
Writing actions
Action functions are regular functions that take three arguments: The current ModelAdmin An HttpRequest representing the current request, A QuerySet containing the set of objects selected by the user.
4.2
5.0
5.1
5.2
6.0
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.
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
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
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
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