djangodocs.org

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

select_related()

Reference QuerySet API reference

select_related(*fields) Returns a QuerySet that will join in the named foreign-key relationships, selecting additional related objects when it executes its query.

dev

extra()

Reference QuerySet API reference

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.

dev

defer()

Reference QuerySet API reference

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.

dev

only()

Reference QuerySet API reference

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.

dev

using()

Reference QuerySet API reference

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:

dev

select_for_update()

Reference QuerySet API reference

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.

dev

raw()

Reference QuerySet API reference

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.

dev

AND (&)

Reference QuerySet API reference

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

dev

OR (|)

Reference QuerySet API reference

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.

dev

XOR (^)

Reference QuerySet API reference

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.

dev

get()

Reference QuerySet API reference

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

dev

get_or_create()

Reference QuerySet API reference

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.

dev

update_or_create()

Reference QuerySet API reference

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.

dev

bulk_update()

Reference QuerySet API reference

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

dev

count()

Reference QuerySet API reference

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

dev

in_bulk()

Reference QuerySet API reference

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.

dev

iterator()

Reference QuerySet API reference

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

dev

latest()

Reference QuerySet API reference

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

dev