djangodocs.org

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

from_queryset()

Topic guide Managers Custom managersCreating a manager with QuerySet methods

classmethodfrom_queryset(queryset_class) For advanced usage you might want both a custom Manager and a custom QuerySet.

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Retrieving objectsRetrieving specific objects with filters

Each time you refine a QuerySet, you get a brand-new QuerySet that is in no way bound to the previous QuerySet. Each refinement creates a separate and distinct QuerySet that can be stored, used and reused.

4.2 5.0 5.1 5.2 6.0

Limiting QuerySets

Topic guide Making queries Retrieving objects

Use a subset of Python’s array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.

4.2 5.0 5.1 5.2 6.0

Topic guide Making queries Retrieving objectsCaching and QuerySets

Querysets do not always cache their results. When evaluating only part of the queryset, the cache is checked, but if it is not populated then the items returned by the subsequent query are not cached.

4.2 5.0 5.1 5.2 6.0

Reference Databases

… | | X | | NO KEY | | | When using select_for_update() on MySQL, make sure you filter a queryset against at least a set of fields contained in unique constraints or only against fields covered by indexes. Otherwise, …

dev

Topic guide Aggregation

Django provides two ways to generate aggregates. The first way is to generate summary values over an entire QuerySet. For example, say you wanted to calculate the average price of all books available for sale.

dev

Topic guide Aggregation

When an aggregation is applied to an empty queryset or grouping, the result defaults to its default parameter, typically None. This behavior occurs because aggregate functions return NULL when the executed query returns no rows.

dev

Topic guide Managers

While most methods from the standard QuerySet are accessible directly from the Manager, this is only the case for the extra methods defined on a custom QuerySet if you also implement them on the Manager: This example allows you to …

dev