Topic guide Making queries
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 object key and index transforms.
1413 sections across all versions Narrow to Django 6.0 (current) →
Topic guide Making queries
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 object 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.
Topic guide Making queries
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
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
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
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.
Topic guide Migrations
… 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
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
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.
FAQ FAQ: The admin
Using the same trick of extracting the user from the request, the get_queryset() and has_change_permission() can be used to control the visibility and editability of objects in the admin.
How-to How to write custom lookups
For instance, if you want to filter a queryset based on the equality of the left and right-hand side insensitively to some SQL function. Let’s examine case-insensitive transformations here.
How-to How to create custom model fields Writing a field subclassUseful methods
If your custom Field class deals with data structures that are more complex than strings, dates, integers, or floats, then you may need to override from_db_value() and to_python().
… Learn more about it below: Models: Introduction to models | Field types | Indexes | Meta options | Model class QuerySets: Making queries | QuerySet method reference | Lookup expressions Model instances: Instance methods | Accessing related objects Migrations: Introduction …
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.
Internals Django Deprecation Timeline
The field_name keyword argument of QuerySet.earliest() and latest() will be removed. See the Django 2.1 release notes for more details on these changes. django.contrib.gis.db.models.functions.ForceRHR will be removed. django.utils.http.cookie_date() will be removed.
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.
Tutorial Writing your first Django app, part 4 Use generic views: Less code is better
This is provided using either the model attribute (in this example, model = Question for DetailView and ResultsView) or by defining the get_queryset() method (as shown in IndexView).