djangodocs.org

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

extra()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

extra(select=None,where=None,params=None,tables=None,order_by=None,select_params=None) Sometimes, the Django query syntax by itself can’t easily express a complex WHERE clause. For these edge cases, Django provides the extra() QuerySet modifier — a hook for injecting specific clauses into the SQL generated by a QuerySet.

defer()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

If you are using the results of a queryset in some situation where you don’t know if you need those particular fields when you initially fetch the data, you can tell Django not to retrieve them from the database.

only()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

only(*fields) The only() method is essentially the opposite of defer(). Only the fields passed into this method and that are not already specified as deferred are loaded immediately when the queryset is evaluated.

using()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

using(alias) This method is for controlling which database the QuerySet will be evaluated against if you are using more than one database. The only argument this method takes is the alias of a database, as defined in DATABASES. For example:

select_for_update()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

select_for_update(nowait=False,skip_locked=False,of=(),no_key=False) Returns a queryset that will lock rows until the end of the transaction, generating a SELECT ... FOR UPDATE SQL statement on supported databases.

raw()

Reference QuerySet API reference QuerySet APIMethods that return new QuerySets

raw(raw_query,params=(),translations=None,using=None) Takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance. This RawQuerySet instance can be iterated over just like a normal QuerySet to provide object instances. See the Performing raw SQL queries for more information.

AND (&)

Reference QuerySet API reference QuerySet APIOperators that return new QuerySets

Combines two QuerySets using the SQL AND operator in a manner similar to chaining filters. The following are equivalent: SQL equivalent:

OR (|)

Reference QuerySet API reference QuerySet APIOperators that return new QuerySets

Combines two QuerySets using the SQL OR operator. The following are equivalent: SQL equivalent: | is not a commutative operation, as different (though equivalent) queries may be generated.

XOR (^)

Reference QuerySet API reference QuerySet APIOperators that return new QuerySets

Combines two QuerySets using the SQL XOR operator. A XOR expression matches rows that are matched by an odd number of operands. The following are equivalent: SQL equivalent: NOTE: XOR is natively supported on MariaDB and MySQL.

get()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

… are guaranteed unique, such as the primary key or fields in a unique constraint. For example: If you expect a queryset to already return one row, you can use get() without any arguments to return the object for that row: …

get_or_create()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

get_or_create(defaults=None,**kwargs) aget_or_create(defaults=None,**kwargs) Asynchronous version: aget_or_create() A convenience method for looking up an object with the given kwargs (may be empty if your model has defaults for all fields), creating one if necessary.

update_or_create()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

update_or_create(defaults=None,create_defaults=None,**kwargs) aupdate_or_create(defaults=None,create_defaults=None,**kwargs) Asynchronous version: aupdate_or_create() A convenience method for updating an object with the given kwargs, creating a new one if necessary. Both create_defaults and defaults are dictionaries of (field, value) pairs.

bulk_update()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

… updates the given fields on the provided model instances, generally with one query, and returns the number of objects updated: QuerySet.update() is used to save the changes, so this is more efficient than iterating through the list of models and …

count()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

count() acount() Asynchronous version: acount() Returns an integer representing the number of objects in the database matching the QuerySet.

in_bulk()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

If id_list isn’t provided, all objects in the queryset are returned. field_name must be a unique field or a distinct field (if there’s only one field specified in distinct()). field_name defaults to the primary key.

iterator()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

iterator(chunk_size=None) aiterator(chunk_size=None) Asynchronous version: aiterator() Evaluates the QuerySet (by performing the query) and returns an iterator (see PEP 234) over the results, or an asynchronous iterator (see PEP 492) if you call its asynchronous version aiterator.

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySetsiterator()

See Isolation when using QuerySet.iterator() for more information. The chunk_size parameter controls the size of batches Django retrieves from the database driver.

latest()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

latest(*fields) alatest(*fields) Asynchronous version: alatest() Returns the latest object in the table based on the given field(s).

earliest()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

earliest(*fields) aearliest(*fields) Asynchronous version: aearliest() Works otherwise like latest() except the direction is changed.

first()

Reference QuerySet API reference QuerySet APIMethods that do not return QuerySets

first() afirst() Asynchronous version: afirst() Returns the first object matched by the queryset, or None if there is no matching object. If the QuerySet has no ordering defined, then the queryset is automatically ordered by the primary key.