djangodocs.org

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

KT() expressions

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.

dev

Deleting objects

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.

dev

Related objects

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

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.

dev

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.

dev

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.

dev

Performing raw queries

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.

dev

Example

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.

dev

Model managers

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: …

dev

TransactionTestCase

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.

dev

The model layer

Django documentation

… 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 …

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.0 5.1 6.0

3.0

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.

6.0

1.8

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

Amend views

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

4.2 5.0 5.1 5.2 6.0