djangodocs.org

680 sections in Django 5.0 Search all versions →

Topic guide Managers Custom managersBase managers

For example, if the Question model from the tutorial had a deleted field and a base manager that filters out instances with deleted=True, a queryset like Choice.objects.filter(question__name__startswith='What') would include choices related to deleted questions.

Topic guide Models

NOTE: The Models Reference Covers all the model related APIs including model fields, related > objects, and QuerySet.

Topic guide Multiple databases Manually selecting a database

For example, say you have a custom manager method that touches the database – User.objects.create_user(). Because create_user() is a manager method, not a QuerySet method, you can’t do User.objects.using('new_users').create_user().

Topic guide Database access optimization Don’t retrieve things you don’t need

If a model has a default ordering (Meta.ordering) and you don’t need it, remove it on a QuerySet by calling order_by() with no parameters. Adding an index to your database may help to improve ordering performance.

Insert in bulk

Topic guide Database access optimization Use bulk methods

When inserting objects into ManyToManyFields, use add() with multiple objects to reduce the number of SQL queries. For example: …is preferable to: …where Bands and Artists have a many-to-many relationship.

Remove in bulk

Topic guide Database access optimization Use bulk methods

When removing objects from ManyToManyFields, use remove() with multiple objects to reduce the number of SQL queries. For example: …is preferable to: …where Bands and Artists have a many-to-many relationship.

Query iteration

Topic guide Making queries Asynchronous queries

… you can swap to async for: Be aware that you also can’t do other things that might iterate over the queryset, such as wrapping list() around it to force its evaluation (you can use async for in a comprehension, if …

KT() expressions

Topic guide Making queries Querying JSONFieldKey, index, and path transforms

New in Django 4.2. 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.

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.

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.

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.

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.

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

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.

TransactionTestCase

Topic guide Testing tools Provided test case classes

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