db.models.query.QuerySet.get()

get(**kwargs) Returns the object matching the given lookup parameters, which should be in the format described in Field lookups. get() raises MultipleObjectsReturned if more than one object was found. The MultipleObjectsReturned exception is an attribute of the model class. get() raises a DoesNotExist exception if an object wasn’t found for the given parameters. This exception is an attribute of the model class. Example: Entry.objects.get(id='foo') # raises Entry.DoesNotExist The DoesNotExi

db.models.query.QuerySet.first()

first() 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. Example: p = Article.objects.order_by('title', 'pub_date').first() Note that first() is a convenience method, the following code sample is equivalent to the above example: try: p = Article.objects.order_by('title', 'pub_date')[0] except IndexError: p = None

db.models.query.QuerySet.filter()

filter(**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. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.

db.models.query.QuerySet.extra()

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. Use this method as a last resort This is an old API that we aim to deprecate at some point in the future. Use it only if you cannot express your query using other quer

db.models.query.QuerySet.exists()

exists() Returns True if the QuerySet contains any results, and False if not. This tries to perform the query in the simplest and fastest way possible, but it does execute nearly the same query as a normal QuerySet query. exists() is useful for searches relating to both object membership in a QuerySet and to the existence of any objects in a QuerySet, particularly in the context of a large QuerySet. The most efficient method of finding whether a model with a unique field (e.g. primary_key) i

db.models.query.QuerySet.earliest()

earliest(field_name=None) Works otherwise like latest() except the direction is changed.

db.models.query.QuerySet.exclude()

exclude(**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. Multiple parameters are joined via AND in the underlying SQL statement, and the whole thing is enclosed in a NOT(). This example excludes all entries whose pub_date is later than 2005-1-3 AND whose headline is “Hello”: Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')

db.models.query.QuerySet.delete()

delete() Performs an SQL delete query on all rows in the QuerySet and returns the number of objects deleted and a dictionary with the number of deletions per object type. The delete() is applied instantly. You cannot call delete() on a QuerySet that has had a slice taken or can otherwise no longer be filtered. For example, to delete all the entries in a particular blog: >>> b = Blog.objects.get(pk=1) # Delete all the entries belonging to this Blog. >>> Entry.objects.filter

db.models.query.QuerySet.distinct()

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. In practice, this is rarely a problem, because simple queries such as Blog.objects.all() don’t introduce the possibility of duplicate result rows. However, if your query spans multiple tables, it’s possible to get duplicate results when a QuerySet is evaluated. That’s when you’d use distinct(). N

db.models.query.QuerySet.defer()

defer(*fields) In some complex data-modeling situations, your models might contain a lot of fields, some of which could contain a lot of data (for example, text fields), or require expensive processing to convert them to Python objects. 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. This is done by passing the names of the field