djangodocs.org

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

Topic guide Managers Custom managers

A Manager’s base QuerySet returns all objects in the system. For example, using this model: …the statement Book.objects.all() will return all books in the database. You can override a Manager’s base QuerySet by overriding the Manager.get_queryset() method.

4.2 5.0 5.1 5.2 6.0

filter()

Reference QuerySet API reference

filter(*args,**kwargs) Returns a new QuerySet containing objects that match the given lookup parameters. The lookup parameters (**kwargs) should be in the format described in Field lookups below. Multiple parameters are joined via AND in the underlying SQL statement.

dev

exclude()

Reference QuerySet API reference

exclude(*args,**kwargs) Returns a new QuerySet containing objects that do not match the given lookup parameters. The lookup parameters (**kwargs) should be in the format described in Field lookups below.

dev

annotate()

Reference QuerySet API reference

annotate(*args,**kwargs) Annotates each object in the QuerySet with the provided list of query expressions or Q() objects.

dev

alias()

Reference QuerySet API reference

alias(*args,**kwargs) Same as annotate(), but instead of annotating objects in the QuerySet, saves the expression for later reuse with other QuerySet methods.

dev

order_by()

Reference QuerySet API reference

order_by(*fields) By default, results returned by a QuerySet are ordered by the ordering tuple given by the ordering option in the model’s Meta. You can override this on a per-QuerySet basis by using the order_by method.

dev

reverse()

Reference QuerySet API reference

reverse() Use the reverse() method to reverse the order in which a queryset’s elements are returned. Calling reverse() a second time restores the ordering back to the normal direction.

dev

distinct()

Reference QuerySet API reference

distinct(*fields) Returns a new QuerySet that uses SELECT DISTINCT in its SQL query. This eliminates duplicate rows from the query results. By default, a QuerySet will not eliminate duplicate rows.

dev

values()

Reference QuerySet API reference

values(*fields,**expressions) Returns a QuerySet that returns dictionaries, rather than model instances, when used as an iterable. Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.

dev

dates()

Reference QuerySet API reference

dates(field,kind,order='ASC') Returns a QuerySet that evaluates to a list of datetime.date objects representing all available dates of a particular kind within the contents of the QuerySet. field should be the name of a DateField of your model.

dev

datetimes()

Reference QuerySet API reference

datetimes(field_name,kind,order='ASC',tzinfo=None) Returns a QuerySet that evaluates to a list of datetime.datetime objects representing all available dates of a particular kind within the contents of the QuerySet. field_name should be the name of a DateTimeField of your model.

dev

none()

Reference QuerySet API reference

none() Calling none() will create a queryset that never returns any objects and no query will be executed when accessing the results. A qs.none() queryset is an instance of EmptyQuerySet. Examples:

dev

all()

Reference QuerySet API reference

all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result.

dev

union()

Reference QuerySet API reference

union(*other_qs,all=False) Uses SQL’s UNION operator to combine the results of two or more QuerySets. For example: The UNION operator selects only distinct values by default. To allow duplicate values, use the all=True argument.

dev

intersection()

Reference QuerySet API reference

intersection(*other_qs) Uses SQL’s INTERSECT operator to return the shared elements of two or more QuerySets. For example: See union() for some restrictions.

dev

difference()

Reference QuerySet API reference

difference(*other_qs) Uses SQL’s EXCEPT operator to keep only elements present in the QuerySet but not in some other QuerySets. For example: See union() for some restrictions.

dev

fetch_mode()

Reference QuerySet API reference

fetch_mode(mode) Returns a QuerySet that sets the given fetch mode for all model instances created by this QuerySet. The fetch mode controls on-demand loading of fields when they are accessed, such as for foreign keys and deferred fields.

dev