QuerySet API reference

该文档描述了 QuerySet API 的细节。它是建立在 模型数据库查询 指南的材料基础上的,因此,在阅读这篇文档之前,您可能需要阅读和理解这些文档。

在整篇参考中,我们将使用 数据库查询指南 中的 Weblog 示例模型

When QuerySets are evaluated

Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset.

You can evaluate a QuerySet in the following ways:

  • Iteration. A QuerySet is iterable, and it executes its database query the first time you iterate over it. For example, this will print the headline of all entries in the database:

    1. for e in Entry.objects.all():
    2. print(e.headline)

    Note: Don’t use this if all you want to do is determine if at least one result exists. It’s more efficient to use exists().

  • Slicing. As explained in 限制 QuerySet 条目数, a QuerySet can be sliced, using Python’s array-slicing syntax. Slicing an unevaluated QuerySet usually returns another unevaluated QuerySet, but Django will execute the database query if you use the “step” parameter of slice syntax, and will return a list. Slicing a QuerySet that has been evaluated also returns a list.

    Also note that even though slicing an unevaluated QuerySet returns another unevaluated QuerySet, modifying it further (e.g., adding more filters, or modifying ordering) is not allowed, since that does not translate well into SQL and it would not have a clear meaning either.

  • Pickling/Caching. See the following section for details of what is involved when pickling QuerySets. The important thing for the purposes of this section is that the results are read from the database.

  • repr(). A QuerySet is evaluated when you call repr() on it. This is for convenience in the Python interactive interpreter, so you can immediately see your results when using the API interactively.

  • len(). A QuerySet is evaluated when you call len() on it. This, as you might expect, returns the length of the result list.

    Note: If you only need to determine the number of records in the set (and don’t need the actual objects), it’s much more efficient to handle a count at the database level using SQL’s SELECT COUNT(*). Django provides a count() method for precisely this reason.

  • list(). Force evaluation of a QuerySet by calling list() on it. For example:

    1. entry_list = list(Entry.objects.all())
  • bool(). Testing a QuerySet in a boolean context, such as using bool(), or, and or an if statement, will cause the query to be executed. If there is at least one result, the QuerySet is True, otherwise False. For example:

    1. if Entry.objects.filter(headline="Test"):
    2. print("There is at least one Entry with the headline Test")

    Note: If you only want to determine if at least one result exists (and don’t need the actual objects), it’s more efficient to use exists().

Pickling QuerySets

If you pickle a QuerySet, this will force all the results to be loaded into memory prior to pickling. Pickling is usually used as a precursor to caching and when the cached queryset is reloaded, you want the results to already be present and ready for use (reading from the database can take some time, defeating the purpose of caching). This means that when you unpickle a QuerySet, it contains the results at the moment it was pickled, rather than the results that are currently in the database.

If you only want to pickle the necessary information to recreate the QuerySet from the database at a later time, pickle the query attribute of the QuerySet. You can then recreate the original QuerySet (without any results loaded) using some code like this:

  1. >>> import pickle
  2. >>> query = pickle.loads(s) # Assuming 's' is the pickled string.
  3. >>> qs = MyModel.objects.all()
  4. >>> qs.query = query # Restore the original 'query'.

The query attribute is an opaque object. It represents the internals of the query construction and is not part of the public API. However, it is safe (and fully supported) to pickle and unpickle the attribute’s contents as described here.

You can’t share pickles between versions

Pickles of QuerySets are only valid for the version of Django that was used to generate them. If you generate a pickle using Django version N, there is no guarantee that pickle will be readable with Django version N+1. Pickles should not be used as part of a long-term archival strategy.

Since pickle compatibility errors can be difficult to diagnose, such as silently corrupted objects, a RuntimeWarning is raised when you try to unpickle a queryset in a Django version that is different than the one in which it was pickled.

QuerySet API

Here’s the formal declaration of a QuerySet:

class QuerySet(model=None, query=None, using=None, hints=None)

Usually when you’ll interact with a QuerySet you’ll use it by chaining filters. To make this work, most QuerySet methods return new querysets. These methods are covered in detail later in this section.

The QuerySet class has two public attributes you can use for introspection:

  • ordered

    True if the QuerySet is ordered — i.e. has an order_by() clause or a default ordering on the model. False otherwise.

  • db

    The database that will be used if this query is executed now.

注解

The query parameter to QuerySet exists so that specialized query subclasses can reconstruct internal query state. The value of the parameter is an opaque representation of that query state and is not part of a public API. To put it another way: if you need to ask, you don’t need to use it.

Methods that return new QuerySets

Django provides a range of QuerySet refinement methods that modify either the types of results returned by the QuerySet or the way its SQL query is executed.

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.

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

  1. Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')

In SQL terms, that evaluates to:

  1. SELECT ...
  2. WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello')

This example excludes all entries whose pub_date is later than 2005-1-3 OR whose headline is “Hello”:

  1. Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello')

In SQL terms, that evaluates to:

  1. SELECT ...
  2. WHERE NOT pub_date > '2005-1-3'
  3. AND NOT headline = 'Hello'

Note the second example is more restrictive.

If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.

annotate()

annotate(\args, **kwargs*)

Annotates each object in the QuerySet with the provided list of query expressions. An expression may be a simple value, a reference to a field on the model (or any related models), or an aggregate expression (averages, sums, etc.) that has been computed over the objects that are related to the objects in the QuerySet.

Each argument to annotate() is an annotation that will be added to each object in the QuerySet that is returned.

The aggregation functions that are provided by Django are described in Aggregation Functions below.

Annotations specified using keyword arguments will use the keyword as the alias for the annotation. Anonymous arguments will have an alias generated for them based upon the name of the aggregate function and the model field that is being aggregated. Only aggregate expressions that reference a single field can be anonymous arguments. Everything else must be a keyword argument.

For example, if you were manipulating a list of blogs, you may want to determine how many entries have been made in each blog:

  1. >>> from django.db.models import Count
  2. >>> q = Blog.objects.annotate(Count('entry'))
  3. # The name of the first blog
  4. >>> q[0].name
  5. 'Blogasaurus'
  6. # The number of entries on the first blog
  7. >>> q[0].entry__count
  8. 42

The Blog model doesn’t define an entry__count attribute by itself, but by using a keyword argument to specify the aggregate function, you can control the name of the annotation:

  1. >>> q = Blog.objects.annotate(number_of_entries=Count('entry'))
  2. # The number of entries on the first blog, using the name provided
  3. >>> q[0].number_of_entries
  4. 42

For an in-depth discussion of aggregation, see the topic guide on Aggregation.

order_by()

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.

举例:

  1. Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')

The result above will be ordered by pub_date descending, then by headline ascending. The negative sign in front of "-pub_date" indicates descending order. Ascending order is implied. To order randomly, use "?", like so:

  1. Entry.objects.order_by('?')

Note: order_by('?') queries may be expensive and slow, depending on the database backend you’re using.

To order by a field in a different model, use the same syntax as when you are querying across model relations. That is, the name of the field, followed by a double underscore (__), followed by the name of the field in the new model, and so on for as many models as you want to join. For example:

  1. Entry.objects.order_by('blog__name', 'headline')

If you try to order by a field that is a relation to another model, Django will use the default ordering on the related model, or order by the related model’s primary key if there is no Meta.ordering specified. For example, since the Blog model has no default ordering specified:

  1. Entry.objects.order_by('blog')

…is identical to:

  1. Entry.objects.order_by('blog__id')

If Blog had ordering = ['name'], then the first queryset would be identical to:

  1. Entry.objects.order_by('blog__name')

You can also order by query expressions by calling asc() or desc() on the expression:

  1. Entry.objects.order_by(Coalesce('summary', 'headline').desc())

asc() and desc() have arguments (nulls_first and nulls_last) that control how null values are sorted.

Be cautious when ordering by fields in related models if you are also using distinct(). See the note in distinct() for an explanation of how related model ordering can change the expected results.

注解

It is permissible to specify a multi-valued field to order the results by (for example, a ManyToManyField field, or the reverse relation of a ForeignKey field).

Consider this case:

  1. class Event(Model):
  2. parent = models.ForeignKey(
  3. 'self',
  4. on_delete=models.CASCADE,
  5. related_name='children',
  6. )
  7. date = models.DateField()
  8. Event.objects.order_by('children__date')

Here, there could potentially be multiple ordering data for each Event; each Event with multiple children will be returned multiple times into the new QuerySet that order_by() creates. In other words, using order_by() on the QuerySet could return more items than you were working on to begin with - which is probably neither expected nor useful.

Thus, take care when using multi-valued field to order the results. If you can be sure that there will only be one ordering piece of data for each of the items you’re ordering, this approach should not present problems. If not, make sure the results are what you expect.

There’s no way to specify whether ordering should be case sensitive. With respect to case-sensitivity, Django will order results however your database backend normally orders them.

You can order by a field converted to lowercase with Lower which will achieve case-consistent ordering:

  1. Entry.objects.order_by(Lower('headline').desc())

If you don’t want any ordering to be applied to a query, not even the default ordering, call order_by() with no parameters.

You can tell if a query is ordered or not by checking the QuerySet.ordered attribute, which will be True if the QuerySet has been ordered in any way.

Each order_by() call will clear any previous ordering. For example, this query will be ordered by pub_date and not headline:

  1. Entry.objects.order_by('headline').order_by('pub_date')

警告

Ordering is not a free operation. Each field you add to the ordering incurs a cost to your database. Each foreign key you add will implicitly include all of its default orderings as well.

If a query doesn’t have an ordering specified, results are returned from the database in an unspecified order. A particular ordering is guaranteed only when ordering by a set of fields that uniquely identify each object in the results. For example, if a name field isn’t unique, ordering by it won’t guarantee objects with the same name always appear in the same order.

reverse()

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.

To retrieve the “last” five items in a queryset, you could do this:

  1. my_queryset.reverse()[:5]

Note that this is not quite the same as slicing from the end of a sequence in Python. The above example will return the last item first, then the penultimate item and so on. If we had a Python sequence and looked at seq[-5:], we would see the fifth-last item first. Django doesn’t support that mode of access (slicing from the end), because it’s not possible to do it efficiently in SQL.

Also, note that reverse() should generally only be called on a QuerySet which has a defined ordering (e.g., when querying against a model which defines a default ordering, or when using order_by()). If no such ordering is defined for a given QuerySet, calling reverse() on it has no real effect (the ordering was undefined prior to calling reverse(), and will remain undefined afterward).

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().

注解

Any fields used in an order_by() call are included in the SQL SELECT columns. This can sometimes lead to unexpected results when used in conjunction with distinct(). If you order by fields from a related model, those fields will be added to the selected columns and they may make otherwise duplicate rows appear to be distinct. Since the extra columns don’t appear in the returned results (they are only there to support ordering), it sometimes looks like non-distinct results are being returned.

Similarly, if you use a values() query to restrict the columns selected, the columns used in any order_by() (or default model ordering) will still be involved and may affect uniqueness of the results.

The moral here is that if you are using distinct() be careful about ordering by related models. Similarly, when using distinct() and values() together, be careful when ordering by fields not in the values() call.

On PostgreSQL only, you can pass positional arguments (*fields) in order to specify the names of fields to which the DISTINCT should apply. This translates to a SELECT DISTINCT ON SQL query. Here’s the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.

注解

When you specify field names, you must provide an order_by() in the QuerySet, and the fields in order_by() must start with the fields in distinct(), in the same order.

For example, SELECT DISTINCT ON (a) gives you the first row for each value in column a. If you don’t specify an order, you’ll get some arbitrary row.

Examples (those after the first will only work on PostgreSQL):

  1. >>> Author.objects.distinct()
  2. [...]
  3. >>> Entry.objects.order_by('pub_date').distinct('pub_date')
  4. [...]
  5. >>> Entry.objects.order_by('blog').distinct('blog')
  6. [...]
  7. >>> Entry.objects.order_by('author', 'pub_date').distinct('author', 'pub_date')
  8. [...]
  9. >>> Entry.objects.order_by('blog__name', 'mod_date').distinct('blog__name', 'mod_date')
  10. [...]
  11. >>> Entry.objects.order_by('author', 'pub_date').distinct('author')
  12. [...]

注解

Keep in mind that order_by() uses any default related model ordering that has been defined. You might have to explicitly order by the relation _id or referenced field to make sure the DISTINCT ON expressions match those at the beginning of the ORDER BY clause. For example, if the Blog model defined an ordering by name:

  1. Entry.objects.order_by('blog').distinct('blog')

…wouldn’t work because the query would be ordered by blog__name thus mismatching the DISTINCT ON expression. You’d have to explicitly order by the relation _id field (blog_id in this case) or the referenced one (blog__pk) to make sure both expressions match.

values()

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.

This example compares the dictionaries of values() with the normal model objects:

  1. # This list contains a Blog object.
  2. >>> Blog.objects.filter(name__startswith='Beatles')
  3. <QuerySet [<Blog: Beatles Blog>]>
  4. # This list contains a dictionary.
  5. >>> Blog.objects.filter(name__startswith='Beatles').values()
  6. <QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]>

The values() method takes optional positional arguments, *fields, which specify field names to which the SELECT should be limited. If you specify the fields, each dictionary will contain only the field keys/values for the fields you specify. If you don’t specify the fields, each dictionary will contain a key and value for every field in the database table.

举例:

  1. >>> Blog.objects.values()
  2. <QuerySet [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]>
  3. >>> Blog.objects.values('id', 'name')
  4. <QuerySet [{'id': 1, 'name': 'Beatles Blog'}]>

The values() method also takes optional keyword arguments, **expressions, which are passed through to annotate():

  1. >>> from django.db.models.functions import Lower
  2. >>> Blog.objects.values(lower_name=Lower('name'))
  3. <QuerySet [{'lower_name': 'beatles blog'}]>

You can use built-in and custom lookups in ordering. For example:

  1. >>> from django.db.models import CharField
  2. >>> from django.db.models.functions import Lower
  3. >>> CharField.register_lookup(Lower)
  4. >>> Blog.objects.values('name__lower')
  5. <QuerySet [{'name__lower': 'beatles blog'}]>

An aggregate within a values() clause is applied before other arguments within the same values() clause. If you need to group by another value, add it to an earlier values() clause instead. For example:

  1. >>> from django.db.models import Count
  2. >>> Blog.objects.values('entry__authors', entries=Count('entry'))
  3. <QuerySet [{'entry__authors': 1, 'entries': 20}, {'entry__authors': 1, 'entries': 13}]>
  4. >>> Blog.objects.values('entry__authors').annotate(entries=Count('entry'))
  5. <QuerySet [{'entry__authors': 1, 'entries': 33}]>

A few subtleties that are worth mentioning:

  • If you have a field called foo that is a ForeignKey, the default values() call will return a dictionary key called foo_id, since this is the name of the hidden model attribute that stores the actual value (the foo attribute refers to the related model). When you are calling values() and passing in field names, you can pass in either foo or foo_id and you will get back the same thing (the dictionary key will match the field name you passed in).

    例子:

    1. >>> Entry.objects.values()
    2. <QuerySet [{'blog_id': 1, 'headline': 'First Entry', ...}, ...]>
    3. >>> Entry.objects.values('blog')
    4. <QuerySet [{'blog': 1}, ...]>
    5. >>> Entry.objects.values('blog_id')
    6. <QuerySet [{'blog_id': 1}, ...]>
  • When using values() together with distinct(), be aware that ordering can affect the results. See the note in distinct() for details.

  • If you use a values() clause after an extra() call, any fields defined by a select argument in the extra() must be explicitly included in the values() call. Any extra() call made after a values() call will have its extra selected fields ignored.

  • Calling only() and defer() after values() doesn’t make sense, so doing so will raise a NotImplementedError.

  • Combining transforms and aggregates requires the use of two annotate() calls, either explicitly or as keyword arguments to values(). As above, if the transform has been registered on the relevant field type the first annotate() can be omitted, thus the following examples are equivalent:

    1. >>> from django.db.models import CharField, Count
    2. >>> from django.db.models.functions import Lower
    3. >>> CharField.register_lookup(Lower)
    4. >>> Blog.objects.values('entry__authors__name__lower').annotate(entries=Count('entry'))
    5. <QuerySet [{'entry__authors__name__lower': 'test author', 'entries': 33}]>
    6. >>> Blog.objects.values(
    7. ... entry__authors__name__lower=Lower('entry__authors__name')
    8. ... ).annotate(entries=Count('entry'))
    9. <QuerySet [{'entry__authors__name__lower': 'test author', 'entries': 33}]>
    10. >>> Blog.objects.annotate(
    11. ... entry__authors__name__lower=Lower('entry__authors__name')
    12. ... ).values('entry__authors__name__lower').annotate(entries=Count('entry'))
    13. <QuerySet [{'entry__authors__name__lower': 'test author', 'entries': 33}]>

It is useful when you know you’re only going to need values from a small number of the available fields and you won’t need the functionality of a model instance object. It’s more efficient to select only the fields you need to use.

Finally, note that you can call filter(), order_by(), etc. after the values() call, that means that these two calls are identical:

  1. Blog.objects.values().order_by('id')
  2. Blog.objects.order_by('id').values()

The people who made Django prefer to put all the SQL-affecting methods first, followed (optionally) by any output-affecting methods (such as values()), but it doesn’t really matter. This is your chance to really flaunt your individualism.

You can also refer to fields on related models with reverse relations through OneToOneField, ForeignKey and ManyToManyField attributes:

  1. >>> Blog.objects.values('name', 'entry__headline')
  2. <QuerySet [{'name': 'My blog', 'entry__headline': 'An entry'},
  3. {'name': 'My blog', 'entry__headline': 'Another entry'}, ...]>

警告

Because ManyToManyField attributes and reverse relations can have multiple related rows, including these can have a multiplier effect on the size of your result set. This will be especially pronounced if you include multiple such fields in your values() query, in which case all possible combinations will be returned.

values_list()

values_list(\fields, flat=False, named=False*)

This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over. Each tuple contains the value from the respective field or expression passed into the values_list() call — so the first item is the first field, etc. For example:

  1. >>> Entry.objects.values_list('id', 'headline')
  2. <QuerySet [(1, 'First entry'), ...]>
  3. >>> from django.db.models.functions import Lower
  4. >>> Entry.objects.values_list('id', Lower('headline'))
  5. <QuerySet [(1, 'first entry'), ...]>

If you only pass in a single field, you can also pass in the flat parameter. If True, this will mean the returned results are single values, rather than one-tuples. An example should make the difference clearer:

  1. >>> Entry.objects.values_list('id').order_by('id')
  2. <QuerySet[(1,), (2,), (3,), ...]>
  3. >>> Entry.objects.values_list('id', flat=True).order_by('id')
  4. <QuerySet [1, 2, 3, ...]>

It is an error to pass in flat when there is more than one field.

You can pass named=True to get results as a namedtuple():

  1. >>> Entry.objects.values_list('id', 'headline', named=True)
  2. <QuerySet [Row(id=1, headline='First entry'), ...]>

Using a named tuple may make use of the results more readable, at the expense of a small performance penalty for transforming the results into a named tuple.

If you don’t pass any values to values_list(), it will return all the fields in the model, in the order they were declared.

A common need is to get a specific field value of a certain model instance. To achieve that, use values_list() followed by a get() call:

  1. >>> Entry.objects.values_list('headline', flat=True).get(pk=1)
  2. 'First entry'

values() and values_list() are both intended as optimizations for a specific use case: retrieving a subset of data without the overhead of creating a model instance. This metaphor falls apart when dealing with many-to-many and other multivalued relations (such as the one-to-many relation of a reverse foreign key) because the “one row, one object” assumption doesn’t hold.

For example, notice the behavior when querying across a ManyToManyField:

  1. >>> Author.objects.values_list('name', 'entry__headline')
  2. <QuerySet [('Noam Chomsky', 'Impressions of Gaza'),
  3. ('George Orwell', 'Why Socialists Do Not Believe in Fun'),
  4. ('George Orwell', 'In Defence of English Cooking'),
  5. ('Don Quixote', None)]>

Authors with multiple entries appear multiple times and authors without any entries have None for the entry headline.

Similarly, when querying a reverse foreign key, None appears for entries not having any author:

  1. >>> Entry.objects.values_list('authors')
  2. <QuerySet [('Noam Chomsky',), ('George Orwell',), (None,)]>

dates()

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. kind should be either "year", "month", "week", or "day". Each datetime.date object in the result list is “truncated” to the given type.

  • "year" returns a list of all distinct year values for the field.
  • "month" returns a list of all distinct year/month values for the field.
  • "week" returns a list of all distinct year/week values for the field. All dates will be a Monday.
  • "day" returns a list of all distinct year/month/day values for the field.

order, which defaults to 'ASC', should be either 'ASC' or 'DESC'. This specifies how to order the results.

举例:

  1. >>> Entry.objects.dates('pub_date', 'year')
  2. [datetime.date(2005, 1, 1)]
  3. >>> Entry.objects.dates('pub_date', 'month')
  4. [datetime.date(2005, 2, 1), datetime.date(2005, 3, 1)]
  5. >>> Entry.objects.dates('pub_date', 'week')
  6. [datetime.date(2005, 2, 14), datetime.date(2005, 3, 14)]
  7. >>> Entry.objects.dates('pub_date', 'day')
  8. [datetime.date(2005, 2, 20), datetime.date(2005, 3, 20)]
  9. >>> Entry.objects.dates('pub_date', 'day', order='DESC')
  10. [datetime.date(2005, 3, 20), datetime.date(2005, 2, 20)]
  11. >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day')
  12. [datetime.date(2005, 3, 20)]

datetimes()

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.

kind should be either "year", "month", "week", "day", "hour", "minute", or "second". Each datetime.datetime object in the result list is “truncated” to the given type.

order, which defaults to 'ASC', should be either 'ASC' or 'DESC'. This specifies how to order the results.

tzinfo defines the time zone to which datetimes are converted prior to truncation. Indeed, a given datetime has different representations depending on the time zone in use. This parameter must be a datetime.tzinfo object. If it’s None, Django uses the current time zone. It has no effect when USE_TZ is False.

注解

This function performs time zone conversions directly in the database. As a consequence, your database must be able to interpret the value of tzinfo.tzname(None). This translates into the following requirements:

none()

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.

举例:

  1. >>> Entry.objects.none()
  2. <QuerySet []>
  3. >>> from django.db.models.query import EmptyQuerySet
  4. >>> isinstance(Entry.objects.none(), EmptyQuerySet)
  5. True

all()

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. After calling all() on either object, you’ll definitely have a QuerySet to work with.

When a QuerySet is evaluated, it typically caches its results. If the data in the database might have changed since a QuerySet was evaluated, you can get updated results for the same query by calling all() on a previously evaluated QuerySet.

union()

union(\other_qs, all=False*)

Uses SQL’s UNION operator to combine the results of two or more QuerySets. For example:

  1. >>> qs1.union(qs2, qs3)

The UNION operator selects only distinct values by default. To allow duplicate values, use the all=True argument.

union(), intersection(), and difference() return model instances of the type of the first QuerySet even if the arguments are QuerySets of other models. Passing different models works as long as the SELECT list is the same in all QuerySets (at least the types, the names don’t matter as long as the types are in the same order). In such cases, you must use the column names from the first QuerySet in QuerySet methods applied to the resulting QuerySet. For example:

  1. >>> qs1 = Author.objects.values_list('name')
  2. >>> qs2 = Entry.objects.values_list('headline')
  3. >>> qs1.union(qs2).order_by('name')

In addition, only LIMIT, OFFSET, COUNT(*), ORDER BY, and specifying columns (i.e. slicing, count(), order_by(), and values()/values_list()) are allowed on the resulting QuerySet. Further, databases place restrictions on what operations are allowed in the combined queries. For example, most databases don’t allow LIMIT or OFFSET in the combined queries.

intersection()

intersection(\other_qs*)

Uses SQL’s INTERSECT operator to return the shared elements of two or more QuerySets. For example:

  1. >>> qs1.intersection(qs2, qs3)

See union() for some restrictions.

difference()

difference(\other_qs*)

Uses SQL’s EXCEPT operator to keep only elements present in the QuerySet but not in some other QuerySets. For example:

  1. >>> qs1.difference(qs2, qs3)

See union() for some restrictions.

select_related()

select_related(\fields*)

Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query. This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.

The following examples illustrate the difference between plain lookups and select_related() lookups. Here’s standard lookup:

  1. # Hits the database.
  2. e = Entry.objects.get(id=5)
  3. # Hits the database again to get the related Blog object.
  4. b = e.blog

And here’s select_related lookup:

  1. # Hits the database.
  2. e = Entry.objects.select_related('blog').get(id=5)
  3. # Doesn't hit the database, because e.blog has been prepopulated
  4. # in the previous query.
  5. b = e.blog

You can use select_related() with any queryset of objects:

  1. from django.utils import timezone
  2. # Find all the blogs with entries scheduled to be published in the future.
  3. blogs = set()
  4. for e in Entry.objects.filter(pub_date__gt=timezone.now()).select_related('blog'):
  5. # Without select_related(), this would make a database query for each
  6. # loop iteration in order to fetch the related blog for each entry.
  7. blogs.add(e.blog)

The order of filter() and select_related() chaining isn’t important. These querysets are equivalent:

  1. Entry.objects.filter(pub_date__gt=timezone.now()).select_related('blog')
  2. Entry.objects.select_related('blog').filter(pub_date__gt=timezone.now())

You can follow foreign keys in a similar way to querying them. If you have the following models:

  1. from django.db import models
  2. class City(models.Model):
  3. # ...
  4. pass
  5. class Person(models.Model):
  6. # ...
  7. hometown = models.ForeignKey(
  8. City,
  9. on_delete=models.SET_NULL,
  10. blank=True,
  11. null=True,
  12. )
  13. class Book(models.Model):
  14. # ...
  15. author = models.ForeignKey(Person, on_delete=models.CASCADE)

… then a call to Book.objects.select_related('author__hometown').get(id=4) will cache the related Person and the related City:

  1. # Hits the database with joins to the author and hometown tables.
  2. b = Book.objects.select_related('author__hometown').get(id=4)
  3. p = b.author # Doesn't hit the database.
  4. c = p.hometown # Doesn't hit the database.
  5. # Without select_related()...
  6. b = Book.objects.get(id=4) # Hits the database.
  7. p = b.author # Hits the database.
  8. c = p.hometown # Hits the database.

You can refer to any ForeignKey or OneToOneField relation in the list of fields passed to select_related().

You can also refer to the reverse direction of a OneToOneField in the list of fields passed to select_related — that is, you can traverse a OneToOneField back to the object on which the field is defined. Instead of specifying the field name, use the related_name for the field on the related object.

There may be some situations where you wish to call select_related() with a lot of related objects, or where you don’t know all of the relations. In these cases it is possible to call select_related() with no arguments. This will follow all non-null foreign keys it can find - nullable foreign keys must be specified. This is not recommended in most cases as it is likely to make the underlying query more complex, and return more data, than is actually needed.

If you need to clear the list of related fields added by past calls of select_related on a QuerySet, you can pass None as a parameter:

  1. >>> without_relations = queryset.select_related(None)

Chaining select_related calls works in a similar way to other methods - that is that select_related('foo', 'bar') is equivalent to select_related('foo').select_related('bar').

prefetch_related()

prefetch_related(\lookups*)

Returns a QuerySet that will automatically retrieve, in a single batch, related objects for each of the specified lookups.

This has a similar purpose to select_related, in that both are designed to stop the deluge of database queries that is caused by accessing related objects, but the strategy is quite different.

select_related works by creating an SQL join and including the fields of the related object in the SELECT statement. For this reason, select_related gets the related objects in the same database query. However, to avoid the much larger result set that would result from joining across a ‘many’ relationship, select_related is limited to single-valued relationships - foreign key and one-to-one.

prefetch_related, on the other hand, does a separate lookup for each relationship, and does the ‘joining’ in Python. This allows it to prefetch many-to-many and many-to-one objects, which cannot be done using select_related, in addition to the foreign key and one-to-one relationships that are supported by select_related. It also supports prefetching of GenericRelation and GenericForeignKey, however, it must be restricted to a homogeneous set of results. For example, prefetching objects referenced by a GenericForeignKey is only supported if the query is restricted to one ContentType.

For example, suppose you have these models:

  1. from django.db import models
  2. class Topping(models.Model):
  3. name = models.CharField(max_length=30)
  4. class Pizza(models.Model):
  5. name = models.CharField(max_length=50)
  6. toppings = models.ManyToManyField(Topping)
  7. def __str__(self):
  8. return "%s (%s)" % (
  9. self.name,
  10. ", ".join(topping.name for topping in self.toppings.all()),
  11. )

and run:

  1. >>> Pizza.objects.all()
  2. ["Hawaiian (ham, pineapple)", "Seafood (prawns, smoked salmon)"...

The problem with this is that every time Pizza.__str__() asks for self.toppings.all() it has to query the database, so Pizza.objects.all() will run a query on the Toppings table for every item in the Pizza QuerySet.

We can reduce to just two queries using prefetch_related:

  1. >>> Pizza.objects.all().prefetch_related('toppings')

This implies a self.toppings.all() for each Pizza; now each time self.toppings.all() is called, instead of having to go to the database for the items, it will find them in a prefetched QuerySet cache that was populated in a single query.

That is, all the relevant toppings will have been fetched in a single query, and used to make QuerySets that have a pre-filled cache of the relevant results; these QuerySets are then used in the self.toppings.all() calls.

The additional queries in prefetch_related() are executed after the QuerySet has begun to be evaluated and the primary query has been executed.

If you have an iterable of model instances, you can prefetch related attributes on those instances using the prefetch_related_objects() function.

Note that the result cache of the primary QuerySet and all specified related objects will then be fully loaded into memory. This changes the typical behavior of QuerySets, which normally try to avoid loading all objects into memory before they are needed, even after a query has been executed in the database.

注解

Remember that, as always with QuerySets, any subsequent chained methods which imply a different database query will ignore previously cached results, and retrieve data using a fresh database query. So, if you write the following:

  1. >>> pizzas = Pizza.objects.prefetch_related('toppings')
  2. >>> [list(pizza.toppings.filter(spicy=True)) for pizza in pizzas]

…then the fact that pizza.toppings.all() has been prefetched will not help you. The prefetch_related('toppings') implied pizza.toppings.all(), but pizza.toppings.filter() is a new and different query. The prefetched cache can’t help here; in fact it hurts performance, since you have done a database query that you haven’t used. So use this feature with caution!

Also, if you call the database-altering methods add(), remove(), clear() or set(), on related managers, any prefetched cache for the relation will be cleared.

You can also use the normal join syntax to do related fields of related fields. Suppose we have an additional model to the example above:

  1. class Restaurant(models.Model):
  2. pizzas = models.ManyToManyField(Pizza, related_name='restaurants')
  3. best_pizza = models.ForeignKey(Pizza, related_name='championed_by', on_delete=models.CASCADE)

The following are all legal:

  1. >>> Restaurant.objects.prefetch_related('pizzas__toppings')

This will prefetch all pizzas belonging to restaurants, and all toppings belonging to those pizzas. This will result in a total of 3 database queries - one for the restaurants, one for the pizzas, and one for the toppings.

  1. >>> Restaurant.objects.prefetch_related('best_pizza__toppings')

This will fetch the best pizza and all the toppings for the best pizza for each restaurant. This will be done in 3 database queries - one for the restaurants, one for the ‘best pizzas’, and one for the toppings.

Of course, the best_pizza relationship could also be fetched using select_related to reduce the query count to 2:

  1. >>> Restaurant.objects.select_related('best_pizza').prefetch_related('best_pizza__toppings')

Since the prefetch is executed after the main query (which includes the joins needed by select_related), it is able to detect that the best_pizza objects have already been fetched, and it will skip fetching them again.

Chaining prefetch_related calls will accumulate the lookups that are prefetched. To clear any prefetch_related behavior, pass None as a parameter:

  1. >>> non_prefetched = qs.prefetch_related(None)

One difference to note when using prefetch_related is that objects created by a query can be shared between the different objects that they are related to i.e. a single Python model instance can appear at more than one point in the tree of objects that are returned. This will normally happen with foreign key relationships. Typically this behavior will not be a problem, and will in fact save both memory and CPU time.

While prefetch_related supports prefetching GenericForeignKey relationships, the number of queries will depend on the data. Since a GenericForeignKey can reference data in multiple tables, one query per table referenced is needed, rather than one query for all the items. There could be additional queries on the ContentType table if the relevant rows have not already been fetched.

prefetch_related in most cases will be implemented using an SQL query that uses the ‘IN’ operator. This means that for a large QuerySet a large ‘IN’ clause could be generated, which, depending on the database, might have performance problems of its own when it comes to parsing or executing the SQL query. Always profile for your use case!

Note that if you use iterator() to run the query, prefetch_related() calls will be ignored since these two optimizations do not make sense together.

You can use the Prefetch object to further control the prefetch operation.

In its simplest form Prefetch is equivalent to the traditional string based lookups:

  1. >>> from django.db.models import Prefetch
  2. >>> Restaurant.objects.prefetch_related(Prefetch('pizzas__toppings'))

You can provide a custom queryset with the optional queryset argument. This can be used to change the default ordering of the queryset:

  1. >>> Restaurant.objects.prefetch_related(
  2. ... Prefetch('pizzas__toppings', queryset=Toppings.objects.order_by('name')))

Or to call select_related() when applicable to reduce the number of queries even further:

  1. >>> Pizza.objects.prefetch_related(
  2. ... Prefetch('restaurants', queryset=Restaurant.objects.select_related('best_pizza')))

You can also assign the prefetched result to a custom attribute with the optional to_attr argument. The result will be stored directly in a list.

This allows prefetching the same relation multiple times with a different QuerySet; for instance:

  1. >>> vegetarian_pizzas = Pizza.objects.filter(vegetarian=True)
  2. >>> Restaurant.objects.prefetch_related(
  3. ... Prefetch('pizzas', to_attr='menu'),
  4. ... Prefetch('pizzas', queryset=vegetarian_pizzas, to_attr='vegetarian_menu'))

Lookups created with custom to_attr can still be traversed as usual by other lookups:

  1. >>> vegetarian_pizzas = Pizza.objects.filter(vegetarian=True)
  2. >>> Restaurant.objects.prefetch_related(
  3. ... Prefetch('pizzas', queryset=vegetarian_pizzas, to_attr='vegetarian_menu'),
  4. ... 'vegetarian_menu__toppings')

Using to_attr is recommended when filtering down the prefetch result as it is less ambiguous than storing a filtered result in the related manager’s cache:

  1. >>> queryset = Pizza.objects.filter(vegetarian=True)
  2. >>>
  3. >>> # Recommended:
  4. >>> restaurants = Restaurant.objects.prefetch_related(
  5. ... Prefetch('pizzas', queryset=queryset, to_attr='vegetarian_pizzas'))
  6. >>> vegetarian_pizzas = restaurants[0].vegetarian_pizzas
  7. >>>
  8. >>> # Not recommended:
  9. >>> restaurants = Restaurant.objects.prefetch_related(
  10. ... Prefetch('pizzas', queryset=queryset))
  11. >>> vegetarian_pizzas = restaurants[0].pizzas.all()

Custom prefetching also works with single related relations like forward ForeignKey or OneToOneField. Generally you’ll want to use select_related() for these relations, but there are a number of cases where prefetching with a custom QuerySet is useful:

  • You want to use a QuerySet that performs further prefetching on related models.

  • You want to prefetch only a subset of the related objects.

  • You want to use performance optimization techniques like deferred fields:

    1. >>> queryset = Pizza.objects.only('name')
    2. >>>
    3. >>> restaurants = Restaurant.objects.prefetch_related(
    4. ... Prefetch('best_pizza', queryset=queryset))

注解

The ordering of lookups matters.

Take the following examples:

  1. >>> prefetch_related('pizzas__toppings', 'pizzas')

This works even though it’s unordered because 'pizzas__toppings' already contains all the needed information, therefore the second argument 'pizzas' is actually redundant.

  1. >>> prefetch_related('pizzas__toppings', Prefetch('pizzas', queryset=Pizza.objects.all()))

This will raise a ValueError because of the attempt to redefine the queryset of a previously seen lookup. Note that an implicit queryset was created to traverse 'pizzas' as part of the 'pizzas__toppings' lookup.

  1. >>> prefetch_related('pizza_list__toppings', Prefetch('pizzas', to_attr='pizza_list'))

This will trigger an AttributeError because 'pizza_list' doesn’t exist yet when 'pizza_list__toppings' is being processed.

This consideration is not limited to the use of Prefetch objects. Some advanced techniques may require that the lookups be performed in a specific order to avoid creating extra queries; therefore it’s recommended to always carefully order prefetch_related arguments.

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 queryset methods. If you do need to use it, please file a ticket using the QuerySet.extra keyword with your use case (please check the list of existing tickets first) so that we can enhance the QuerySet API to allow removing extra(). We are no longer improving or fixing bugs for this method.

For example, this use of extra():

  1. >>> qs.extra(
  2. ... select={'val': "select col from sometable where othercol = %s"},
  3. ... select_params=(someparam,),
  4. ... )

相当于:

  1. >>> qs.annotate(val=RawSQL("select col from sometable where othercol = %s", (someparam,)))

The main benefit of using RawSQL is that you can set output_field if needed. The main downside is that if you refer to some table alias of the queryset in the raw SQL, then it is possible that Django might change that alias (for example, when the queryset is used as a subquery in yet another query).

警告

You should be very careful whenever you use extra(). Every time you use it, you should escape any parameters that the user can control by using params in order to protect against SQL injection attacks.

You also must not quote placeholders in the SQL string. This example is vulnerable to SQL injection because of the quotes around %s:

  1. SELECT col FROM sometable WHERE othercol = '%s' # unsafe!

You can read more about how Django’s SQL injection protection works.

By definition, these extra lookups may not be portable to different database engines (because you’re explicitly writing SQL code) and violate the DRY principle, so you should avoid them if possible.

Specify one or more of params, select, where or tables. None of the arguments is required, but you should use at least one of them.

  • select

    The select argument lets you put extra fields in the SELECT clause. It should be a dictionary mapping attribute names to SQL clauses to use to calculate that attribute.

    举例:

    1. Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})

    As a result, each Entry object will have an extra attribute, is_recent, a boolean representing whether the entry’s pub_date is greater than Jan. 1, 2006.

    Django inserts the given SQL snippet directly into the SELECT statement, so the resulting SQL of the above example would be something like:

    1. SELECT blog_entry.*, (pub_date > '2006-01-01') AS is_recent
    2. FROM blog_entry;

    The next example is more advanced; it does a subquery to give each resulting Blog object an entry_count attribute, an integer count of associated Entry objects:

    1. Blog.objects.extra(
    2. select={
    3. 'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
    4. },
    5. )

    In this particular case, we’re exploiting the fact that the query will already contain the blog_blog table in its FROM clause.

    The resulting SQL of the above example would be:

    1. SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id) AS entry_count
    2. FROM blog_blog;

    Note that the parentheses required by most database engines around subqueries are not required in Django’s select clauses. Also note that some database backends, such as some MySQL versions, don’t support subqueries.

    In some rare cases, you might wish to pass parameters to the SQL fragments in extra(select=...). For this purpose, use the select_params parameter.

    This will work, for example:

    1. Blog.objects.extra(
    2. select={'a': '%s', 'b': '%s'},
    3. select_params=('one', 'two'),
    4. )

    If you need to use a literal %s inside your select string, use the sequence %%s.

  • where / tables

    You can define explicit SQL WHERE clauses — perhaps to perform non-explicit joins — by using where. You can manually add tables to the SQL FROM clause by using tables.

    where and tables both take a list of strings. All where parameters are “AND”ed to any other search criteria.

    举例:

    1. Entry.objects.extra(where=["foo='a' OR bar = 'a'", "baz = 'a'"])

    …translates (roughly) into the following SQL:

    1. SELECT * FROM blog_entry WHERE (foo='a' OR bar='a') AND (baz='a')

    Be careful when using the tables parameter if you’re specifying tables that are already used in the query. When you add extra tables via the tables parameter, Django assumes you want that table included an extra time, if it is already included. That creates a problem, since the table name will then be given an alias. If a table appears multiple times in an SQL statement, the second and subsequent occurrences must use aliases so the database can tell them apart. If you’re referring to the extra table you added in the extra where parameter this is going to cause errors.

    Normally you’ll only be adding extra tables that don’t already appear in the query. However, if the case outlined above does occur, there are a few solutions. First, see if you can get by without including the extra table and use the one already in the query. If that isn’t possible, put your extra() call at the front of the queryset construction so that your table is the first use of that table. Finally, if all else fails, look at the query produced and rewrite your where addition to use the alias given to your extra table. The alias will be the same each time you construct the queryset in the same way, so you can rely upon the alias name to not change.

  • order_by

    If you need to order the resulting queryset using some of the new fields or tables you have included via extra() use the order_by parameter to extra() and pass in a sequence of strings. These strings should either be model fields (as in the normal order_by() method on querysets), of the form table_name.column_name or an alias for a column that you specified in the select parameter to extra().

    例子:

    1. q = Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
    2. q = q.extra(order_by = ['-is_recent'])

    This would sort all the items for which is_recent is true to the front of the result set (True sorts before False in a descending ordering).

    This shows, by the way, that you can make multiple calls to extra() and it will behave as you expect (adding new constraints each time).

  • params

    The where parameter described above may use standard Python database string placeholders — '%s' to indicate parameters the database engine should automatically quote. The params argument is a list of any extra parameters to be substituted.

    举例:

    1. Entry.objects.extra(where=['headline=%s'], params=['Lennon'])

    Always use params instead of embedding values directly into where because params will ensure values are quoted correctly according to your particular backend. For example, quotes will be escaped correctly.

    Bad:

    1. Entry.objects.extra(where=["headline='Lennon'"])

    Good:

    1. Entry.objects.extra(where=['headline=%s'], params=['Lennon'])

警告

若你在 MySQL 上执行查询,至于其无声的强制类型可能会弄混类型时导致不可预料的后果。若你用一个整数值查询一个字符串列,MySQL 会执行比较前将表中所有数据强制转为整数。例如,若数据表包含的值有 'abc''def',而查询语句为 WHERE mycolumn=0,这两行都会匹配上。要避免这种情况,在将值传给查询语句前进行合适的类型转换。

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 fields to not load to defer():

  1. Entry.objects.defer("headline", "body")

A queryset that has deferred fields will still return model instances. Each deferred field will be retrieved from the database if you access that field (one at a time, not all the deferred fields at once).

You can make multiple calls to defer(). Each call adds new fields to the deferred set:

  1. # Defers both the body and headline fields.
  2. Entry.objects.defer("body").filter(rating=5).defer("headline")

The order in which fields are added to the deferred set does not matter. Calling defer() with a field name that has already been deferred is harmless (the field will still be deferred).

You can defer loading of fields in related models (if the related models are loading via select_related()) by using the standard double-underscore notation to separate related fields:

  1. Blog.objects.select_related().defer("entry__headline", "entry__body")

If you want to clear the set of deferred fields, pass None as a parameter to defer():

  1. # Load all fields immediately.
  2. my_queryset.defer(None)

Some fields in a model won’t be deferred, even if you ask for them. You can never defer the loading of the primary key. If you are using select_related() to retrieve related models, you shouldn’t defer the loading of the field that connects from the primary model to the related one, doing so will result in an error.

注解

The defer() method (and its cousin, only(), below) are only for advanced use-cases. They provide an optimization for when you have analyzed your queries closely and understand exactly what information you need and have measured that the difference between returning the fields you need and the full set of fields for the model will be significant.

Even if you think you are in the advanced use-case situation, only use defer() when you cannot, at queryset load time, determine if you will need the extra fields or not. If you are frequently loading and using a particular subset of your data, the best choice you can make is to normalize your models and put the non-loaded data into a separate model (and database table). If the columns must stay in the one table for some reason, create a model with Meta.managed = False (see the managed attribute documentation) containing just the fields you normally need to load and use that where you might otherwise call defer(). This makes your code more explicit to the reader, is slightly faster and consumes a little less memory in the Python process.

For example, both of these models use the same underlying database table:

  1. class CommonlyUsedModel(models.Model):
  2. f1 = models.CharField(max_length=10)
  3. class Meta:
  4. managed = False
  5. db_table = 'app_largetable'
  6. class ManagedModel(models.Model):
  7. f1 = models.CharField(max_length=10)
  8. f2 = models.CharField(max_length=10)
  9. class Meta:
  10. db_table = 'app_largetable'
  11. # Two equivalent QuerySets:
  12. CommonlyUsedModel.objects.all()
  13. ManagedModel.objects.all().defer('f2')

If many fields need to be duplicated in the unmanaged model, it may be best to create an abstract model with the shared fields and then have the unmanaged and managed models inherit from the abstract model.

注解

When calling save() for instances with deferred fields, only the loaded fields will be saved. See save() for more details.

only()

only(\fields*)

The only() method is more or less the opposite of defer(). You call it with the fields that should not be deferred when retrieving a model. If you have a model where almost all the fields need to be deferred, using only() to specify the complementary set of fields can result in simpler code.

Suppose you have a model with fields name, age and biography. The following two querysets are the same, in terms of deferred fields:

  1. Person.objects.defer("age", "biography")
  2. Person.objects.only("name")

Whenever you call only() it replaces the set of fields to load immediately. The method’s name is mnemonic: only those fields are loaded immediately; the remainder are deferred. Thus, successive calls to only() result in only the final fields being considered:

  1. # This will defer all fields except the headline.
  2. Entry.objects.only("body", "rating").only("headline")

Since defer() acts incrementally (adding fields to the deferred list), you can combine calls to only() and defer() and things will behave logically:

  1. # Final result is that everything except "headline" is deferred.
  2. Entry.objects.only("headline", "body").defer("body")
  3. # Final result loads headline and body immediately (only() replaces any
  4. # existing set of fields).
  5. Entry.objects.defer("body").only("headline", "body")

All of the cautions in the note for the defer() documentation apply to only() as well. Use it cautiously and only after exhausting your other options.

Using only() and omitting a field requested using select_related() is an error as well.

注解

When calling save() for instances with deferred fields, only the loaded fields will be saved. See save() for more details.

using()

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.

例子:

  1. # queries the database with the 'default' alias.
  2. >>> Entry.objects.all()
  3. # queries the database with the 'backup' alias
  4. >>> Entry.objects.using('backup')

select_for_update()

select_for_update(nowait=False, skip_locked=False, of=())

Returns a queryset that will lock rows until the end of the transaction, generating a SELECT ... FOR UPDATE SQL statement on supported databases.

例子:

  1. from django.db import transaction
  2. entries = Entry.objects.select_for_update().filter(author=request.user)
  3. with transaction.atomic():
  4. for entry in entries:
  5. ...

When the queryset is evaluated (for entry in entries in this case), all matched entries will be locked until the end of the transaction block, meaning that other transactions will be prevented from changing or acquiring locks on them.

Usually, if another transaction has already acquired a lock on one of the selected rows, the query will block until the lock is released. If this is not the behavior you want, call select_for_update(nowait=True). This will make the call non-blocking. If a conflicting lock is already acquired by another transaction, DatabaseError will be raised when the queryset is evaluated. You can also ignore locked rows by using select_for_update(skip_locked=True) instead. The nowait and skip_locked are mutually exclusive and attempts to call select_for_update() with both options enabled will result in a ValueError.

By default, select_for_update() locks all rows that are selected by the query. For example, rows of related objects specified in select_related() are locked in addition to rows of the queryset’s model. If this isn’t desired, specify the related objects you want to lock in select_for_update(of=(...)) using the same fields syntax as select_related(). Use the value 'self' to refer to the queryset’s model.

Lock parents models in select_for_update(of=(...))

If you want to lock parents models when using multi-table inheritance, you must specify parent link fields (by default <parent_model_name>_ptr) in the of argument. For example:

  1. Restaurant.objects.select_for_update(of=('self', 'place_ptr'))

You can’t use select_for_update() on nullable relations:

  1. >>> Person.objects.select_related('hometown').select_for_update()
  2. Traceback (most recent call last):
  3. ...
  4. django.db.utils.NotSupportedError: FOR UPDATE cannot be applied to the nullable side of an outer join

To avoid that restriction, you can exclude null objects if you don’t care about them:

  1. >>> Person.objects.select_related('hometown').select_for_update().exclude(hometown=None)
  2. <QuerySet [<Person: ...)>, ...]>

Currently, the postgresql, oracle, and mysql database backends support select_for_update(). However, MariaDB 10.3+ supports only the nowait argument and MySQL 8.0.1+ supports the nowait and skip_locked arguments. MySQL and MariaDB don’t support the of argument.

Passing nowait=True, skip_locked=True, or of to select_for_update() using database backends that do not support these options, such as MySQL, raises a NotSupportedError. This prevents code from unexpectedly blocking.

Evaluating a queryset with select_for_update() in autocommit mode on backends which support SELECT ... FOR UPDATE is a TransactionManagementError error because the rows are not locked in that case. If allowed, this would facilitate data corruption and could easily be caused by calling code that expects to be run in a transaction outside of one.

Using select_for_update() on backends which do not support SELECT ... FOR UPDATE (such as SQLite) will have no effect. SELECT ... FOR UPDATE will not be added to the query, and an error isn’t raised if select_for_update() is used in autocommit mode.

警告

Although select_for_update() normally fails in autocommit mode, since TestCase automatically wraps each test in a transaction, calling select_for_update() in a TestCase even outside an atomic() block will (perhaps unexpectedly) pass without raising a TransactionManagementError. To properly test select_for_update() you should use TransactionTestCase.

Certain expressions may not be supported

PostgreSQL doesn’t support select_for_update() with Window expressions.

raw()

raw(raw_query, params=None, translations=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 执行原生 SQL 查询 for more information.

警告

raw() always triggers a new query and doesn’t account for previous filtering. As such, it should generally be called from the Manager or from a fresh QuerySet instance.

Operators that return new QuerySets

Combined querysets must use the same model.

AND (&)

Combines two QuerySets using the SQL AND operator.

The following are equivalent:

  1. Model.objects.filter(x=1) & Model.objects.filter(y=2)
  2. Model.objects.filter(x=1, y=2)
  3. from django.db.models import Q
  4. Model.objects.filter(Q(x=1) & Q(y=2))

SQL equivalent:

  1. SELECT ... WHERE x=1 AND y=2

OR (|)

Combines two QuerySets using the SQL OR operator.

The following are equivalent:

  1. Model.objects.filter(x=1) | Model.objects.filter(y=2)
  2. from django.db.models import Q
  3. Model.objects.filter(Q(x=1) | Q(y=2))

SQL equivalent:

  1. SELECT ... WHERE x=1 OR y=2

Methods that do not return QuerySets

The following QuerySet methods evaluate the QuerySet and return something other than a QuerySet.

These methods do not use a cache (see 缓存和 QuerySet). Rather, they query the database each time they’re called.

get()

get(\*kwargs*)

Returns the object matching the given lookup parameters, which should be in the format described in Field lookups. You should use lookups that are guaranteed unique, such as the primary key or fields in a unique constraint. For example:

  1. Entry.objects.get(id=1)
  2. Entry.objects.get(blog=blog, entry_number=1)

If you expect a queryset to already return one row, you can use get() without any arguments to return the object for that row:

  1. Entry.objects.filter(pk=1).get()

If get() doesn’t find any object, it raises a Model.DoesNotExist exception:

  1. Entry.objects.get(id=-999) # raises Entry.DoesNotExist

If get() finds more than one object, it raises a Model.MultipleObjectsReturned exception:

  1. Entry.objects.get(name='A Duplicated Name') # raises Entry.MultipleObjectsReturned

Both these exception classes are attributes of the model class, and specific to that model. If you want to handle such exceptions from several get() calls for different models, you can use their generic base classes. For example, you can use django.core.exceptions.ObjectDoesNotExist to handle DoesNotExist exceptions from multiple models:

  1. from django.core.exceptions import ObjectDoesNotExist
  2. try:
  3. blog = Blog.objects.get(id=1)
  4. entry = Entry.objects.get(blog=blog, entry_number=1)
  5. except ObjectDoesNotExist:
  6. print("Either the blog or entry doesn't exist.")

create()

create(\*kwargs*)

A convenience method for creating an object and saving it all in one step. Thus:

  1. p = Person.objects.create(first_name="Bruce", last_name="Springsteen")

和:

  1. p = Person(first_name="Bruce", last_name="Springsteen")
  2. p.save(force_insert=True)

are equivalent.

The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that you set and if that value already exists in the database, a call to create() will fail with an IntegrityError since primary keys must be unique. Be prepared to handle the exception if you are using manual primary keys.

get_or_create()

get_or_create(defaults=None, \*kwargs*)

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.

Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

This is meant to prevent duplicate objects from being created when requests are made in parallel, and as a shortcut to boilerplatish code. For example:

  1. try:
  2. obj = Person.objects.get(first_name='John', last_name='Lennon')
  3. except Person.DoesNotExist:
  4. obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
  5. obj.save()

Here, with concurrent requests, multiple attempts to save a Person with the same parameters may be made. To avoid this race condition, the above example can be rewritten using get_or_create() like so:

  1. obj, created = Person.objects.get_or_create(
  2. first_name='John',
  3. last_name='Lennon',
  4. defaults={'birthday': date(1940, 10, 9)},
  5. )

Any keyword arguments passed to get_or_create()except an optional one called defaults — will be used in a get() call. If an object is found, get_or_create() returns a tuple of that object and False.

警告

This method is atomic assuming that the database enforces uniqueness of the keyword arguments (see unique or unique_together). If the fields used in the keyword arguments do not have a uniqueness constraint, concurrent calls to this method may result in multiple rows with the same parameters being inserted.

You can specify more complex conditions for the retrieved object by chaining get_or_create() with filter() and using Q objects. For example, to retrieve Robert or Bob Marley if either exists, and create the latter otherwise:

  1. from django.db.models import Q
  2. obj, created = Person.objects.filter(
  3. Q(first_name='Bob') | Q(first_name='Robert'),
  4. ).get_or_create(last_name='Marley', defaults={'first_name': 'Bob'})

If multiple objects are found, get_or_create() raises MultipleObjectsReturned. If an object is not found, get_or_create() will instantiate and save a new object, returning a tuple of the new object and True. The new object will be created roughly according to this algorithm:

  1. params = {k: v for k, v in kwargs.items() if '__' not in k}
  2. params.update({k: v() if callable(v) else v for k, v in defaults.items()})
  3. obj = self.model(**params)
  4. obj.save()

In English, that means start with any non-'defaults' keyword argument that doesn’t contain a double underscore (which would indicate a non-exact lookup). Then add the contents of defaults, overriding any keys if necessary, and use the result as the keyword arguments to the model class. If there are any callables in defaults, evaluate them. As hinted at above, this is a simplification of the algorithm that is used, but it contains all the pertinent details. The internal implementation has some more error-checking than this and handles some extra edge-conditions; if you’re interested, read the code.

If you have a field named defaults and want to use it as an exact lookup in get_or_create(), use 'defaults__exact', like so:

  1. Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'})

The get_or_create() method has similar error behavior to create() when you’re using manually specified primary keys. If an object needs to be created and the key already exists in the database, an IntegrityError will be raised.

Finally, a word on using get_or_create() in Django views. Please make sure to use it only in POST requests unless you have a good reason not to. GET requests shouldn’t have any effect on data. Instead, use POST whenever a request to a page has a side effect on your data. For more, see Safe methods in the HTTP spec.

警告

You can use get_or_create() through ManyToManyField attributes and reverse relations. In that case you will restrict the queries inside the context of that relation. That could lead you to some integrity problems if you don’t use it consistently.

Being the following models:

  1. class Chapter(models.Model):
  2. title = models.CharField(max_length=255, unique=True)
  3. class Book(models.Model):
  4. title = models.CharField(max_length=256)
  5. chapters = models.ManyToManyField(Chapter)

You can use get_or_create() through Book’s chapters field, but it only fetches inside the context of that book:

  1. >>> book = Book.objects.create(title="Ulysses")
  2. >>> book.chapters.get_or_create(title="Telemachus")
  3. (<Chapter: Telemachus>, True)
  4. >>> book.chapters.get_or_create(title="Telemachus")
  5. (<Chapter: Telemachus>, False)
  6. >>> Chapter.objects.create(title="Chapter 1")
  7. <Chapter: Chapter 1>
  8. >>> book.chapters.get_or_create(title="Chapter 1")
  9. # Raises IntegrityError

This is happening because it’s trying to get or create “Chapter 1” through the book “Ulysses”, but it can’t do any of them: the relation can’t fetch that chapter because it isn’t related to that book, but it can’t create it either because title field should be unique.

update_or_create()

update_or_create(defaults=None, \*kwargs*)

A convenience method for updating an object with the given kwargs, creating a new one if necessary. The defaults is a dictionary of (field, value) pairs used to update the object. The values in defaults can be callables.

Returns a tuple of (object, created), where object is the created or updated object and created is a boolean specifying whether a new object was created.

The update_or_create method tries to fetch an object from database based on the given kwargs. If a match is found, it updates the fields passed in the defaults dictionary.

This is meant as a shortcut to boilerplatish code. For example:

  1. defaults = {'first_name': 'Bob'}
  2. try:
  3. obj = Person.objects.get(first_name='John', last_name='Lennon')
  4. for key, value in defaults.items():
  5. setattr(obj, key, value)
  6. obj.save()
  7. except Person.DoesNotExist:
  8. new_values = {'first_name': 'John', 'last_name': 'Lennon'}
  9. new_values.update(defaults)
  10. obj = Person(**new_values)
  11. obj.save()

This pattern gets quite unwieldy as the number of fields in a model goes up. The above example can be rewritten using update_or_create() like so:

  1. obj, created = Person.objects.update_or_create(
  2. first_name='John', last_name='Lennon',
  3. defaults={'first_name': 'Bob'},
  4. )

For detailed description how names passed in kwargs are resolved see get_or_create().

As described above in get_or_create(), this method is prone to a race-condition which can result in multiple rows being inserted simultaneously if uniqueness is not enforced at the database level.

Like get_or_create() and create(), if you’re using manually specified primary keys and an object needs to be created but the key already exists in the database, an IntegrityError is raised.

bulk_create()

bulk_create(objs, batch_size=None, ignore_conflicts=False)

This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are):

  1. >>> Entry.objects.bulk_create([
  2. ... Entry(headline='This is a test'),
  3. ... Entry(headline='This is only a test'),
  4. ... ])

This has a number of caveats though:

  • The model’s save() method will not be called, and the pre_save and post_save signals will not be sent.

  • It does not work with child models in a multi-table inheritance scenario.

  • If the model’s primary key is an AutoField it does not retrieve and set the primary key attribute, as save() does, unless the database backend supports it (currently PostgreSQL).

  • It does not work with many-to-many relationships.

  • It casts objs to a list, which fully evaluates objs if it’s a generator. The cast allows inspecting all objects so that any objects with a manually set primary key can be inserted first. If you want to insert objects in batches without evaluating the entire generator at once, you can use this technique as long as the objects don’t have any manually set primary keys:

    1. from itertools import islice
    2. batch_size = 100
    3. objs = (Entry(headline='Test %s' % i) for i in range(1000))
    4. while True:
    5. batch = list(islice(objs, batch_size))
    6. if not batch:
    7. break
    8. Entry.objects.bulk_create(batch, batch_size)

The batch_size parameter controls how many objects are created in a single query. The default is to create all objects in one batch, except for SQLite where the default is such that at most 999 variables per query are used.

On databases that support it (all but Oracle), setting the ignore_conflicts parameter to True tells the database to ignore failure to insert any rows that fail constraints such as duplicate unique values. Enabling this parameter disables setting the primary key on each model instance (if the database normally supports it).

警告

On MySQL and MariaDB, setting the ignore_conflicts parameter to True turns certain types of errors, other than duplicate key, into warnings. Even with Strict Mode. For example: invalid values or non-nullable violations. See the MySQL documentation and MariaDB documentation for more details.

Changed in Django 2.2:

The ignore_conflicts parameter was added.

bulk_update()

New in Django 2.2.

bulk_update(objs, fields, batch_size=None)

This method efficiently updates the given fields on the provided model instances, generally with one query:

  1. >>> objs = [
  2. ... Entry.objects.create(headline='Entry 1'),
  3. ... Entry.objects.create(headline='Entry 2'),
  4. ... ]
  5. >>> objs[0].headline = 'This is entry 1'
  6. >>> objs[1].headline = 'This is entry 2'
  7. >>> Entry.objects.bulk_update(objs, ['headline'])

QuerySet.update() is used to save the changes, so this is more efficient than iterating through the list of models and calling save() on each of them, but it has a few caveats:

  • You cannot update the model’s primary key.
  • Each model’s save() method isn’t called, and the pre_save and post_save signals aren’t sent.
  • If updating a large number of columns in a large number of rows, the SQL generated can be very large. Avoid this by specifying a suitable batch_size.
  • Updating fields defined on multi-table inheritance ancestors will incur an extra query per ancestor.
  • If objs contains duplicates, only the first one is updated.

The batch_size parameter controls how many objects are saved in a single query. The default is to update all objects in one batch, except for SQLite and Oracle which have restrictions on the number of variables used in a query.

count()

count()

Returns an integer representing the number of objects in the database matching the QuerySet.

举例:

  1. # Returns the total number of entries in the database.
  2. Entry.objects.count()
  3. # Returns the number of entries whose headline contains 'Lennon'
  4. Entry.objects.filter(headline__contains='Lennon').count()

A count() call performs a SELECT COUNT(*) behind the scenes, so you should always use count() rather than loading all of the record into Python objects and calling len() on the result (unless you need to load the objects into memory anyway, in which case len() will be faster).

Note that if you want the number of items in a QuerySet and are also retrieving model instances from it (for example, by iterating over it), it’s probably more efficient to use len(queryset) which won’t cause an extra database query like count() would.

in_bulk()

in_bulk(id_list=None, field_name=’pk’)

Takes a list of field values (id_list) and the field_name for those values, and returns a dictionary mapping each value to an instance of the object with the given field value. If id_list isn’t provided, all objects in the queryset are returned. field_name must be a unique field, and it defaults to the primary key.

举例:

  1. >>> Blog.objects.in_bulk([1])
  2. {1: <Blog: Beatles Blog>}
  3. >>> Blog.objects.in_bulk([1, 2])
  4. {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>}
  5. >>> Blog.objects.in_bulk([])
  6. {}
  7. >>> Blog.objects.in_bulk()
  8. {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>, 3: <Blog: Django Weblog>}
  9. >>> Blog.objects.in_bulk(['beatles_blog'], field_name='slug')
  10. {'beatles_blog': <Blog: Beatles Blog>}

If you pass in_bulk() an empty list, you’ll get an empty dictionary.

iterator()

iterator(chunk_size=2000)

Evaluates the QuerySet (by performing the query) and returns an iterator (see PEP 234) over the results. A QuerySet typically caches its results internally so that repeated evaluations do not result in additional queries. In contrast, iterator() will read results directly, without doing any caching at the QuerySet level (internally, the default iterator calls iterator() and caches the return value). For a QuerySet which returns a large number of objects that you only need to access once, this can result in better performance and a significant reduction in memory.

Note that using iterator() on a QuerySet which has already been evaluated will force it to evaluate again, repeating the query.

Also, use of iterator() causes previous prefetch_related() calls to be ignored since these two optimizations do not make sense together.

Depending on the database backend, query results will either be loaded all at once or streamed from the database using server-side cursors.

With server-side cursors

Oracle and PostgreSQL use server-side cursors to stream results from the database without loading the entire result set into memory.

The Oracle database driver always uses server-side cursors.

With server-side cursors, the chunk_size parameter specifies the number of results to cache at the database driver level. Fetching bigger chunks diminishes the number of round trips between the database driver and the database, at the expense of memory.

On PostgreSQL, server-side cursors will only be used when the DISABLE_SERVER_SIDE_CURSORS setting is False. Read Transaction pooling and server-side cursors if you’re using a connection pooler configured in transaction pooling mode. When server-side cursors are disabled, the behavior is the same as databases that don’t support server-side cursors.

Without server-side cursors

MySQL doesn’t support streaming results, hence the Python database driver loads the entire result set into memory. The result set is then transformed into Python row objects by the database adapter using the fetchmany() method defined in PEP 249.

SQLite can fetch results in batches using fetchmany(), but since SQLite doesn’t provide isolation between queries within a connection, be careful when writing to the table being iterated over. See Isolation when using QuerySet.iterator() for more information.

The chunk_size parameter controls the size of batches Django retrieves from the database driver. Larger batches decrease the overhead of communicating with the database driver at the expense of a slight increase in memory consumption.

The default value of chunk_size, 2000, comes from a calculation on the psycopg mailing list:

Assuming rows of 10-20 columns with a mix of textual and numeric data, 2000 is going to fetch less than 100KB of data, which seems a good compromise between the number of rows transferred and the data discarded if the loop is exited early.

Changed in Django 2.2:

Support for result streaming on SQLite was added.

latest()

latest(\fields*)

Returns the latest object in the table based on the given field(s).

This example returns the latest Entry in the table, according to the pub_date field:

  1. Entry.objects.latest('pub_date')

You can also choose the latest based on several fields. For example, to select the Entry with the earliest expire_date when two entries have the same pub_date:

  1. Entry.objects.latest('pub_date', '-expire_date')

The negative sign in '-expire_date' means to sort expire_date in descending order. Since latest() gets the last result, the Entry with the earliest expire_date is selected.

If your model’s Meta specifies get_latest_by, you can omit any arguments to earliest() or latest(). The fields specified in get_latest_by will be used by default.

Like get(), earliest() and latest() raise DoesNotExist if there is no object with the given parameters.

Note that earliest() and latest() exist purely for convenience and readability.

earliest() and latest() may return instances with null dates.

Since ordering is delegated to the database, results on fields that allow null values may be ordered differently if you use different databases. For example, PostgreSQL and MySQL sort null values as if they are higher than non-null values, while SQLite does the opposite.

You may want to filter out null values:

  1. Entry.objects.filter(pub_date__isnull=False).latest('pub_date')

earliest()

earliest(\fields*)

Works otherwise like latest() except the direction is changed.

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. This can affect aggregation results as described in 与默认排序或 order_by() 交互.

举例:

  1. 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:

  1. try:
  2. p = Article.objects.order_by('title', 'pub_date')[0]
  3. except IndexError:
  4. p = None

last()

last()

Works like first(), but returns the last object in the queryset.

aggregate()

aggregate(\args, **kwargs*)

Returns a dictionary of aggregate values (averages, sums, etc.) calculated over the QuerySet. Each argument to aggregate() specifies a value that will be included in the dictionary that is returned.

The aggregation functions that are provided by Django are described in Aggregation Functions below. Since aggregates are also query expressions, you may combine aggregates with other aggregates or values to create complex aggregates.

Aggregates specified using keyword arguments will use the keyword as the name for the annotation. Anonymous arguments will have a name generated for them based upon the name of the aggregate function and the model field that is being aggregated. Complex aggregates cannot use anonymous arguments and must specify a keyword argument as an alias.

For example, when you are working with blog entries, you may want to know the number of authors that have contributed blog entries:

  1. >>> from django.db.models import Count
  2. >>> q = Blog.objects.aggregate(Count('entry'))
  3. {'entry__count': 16}

By using a keyword argument to specify the aggregate function, you can control the name of the aggregation value that is returned:

  1. >>> q = Blog.objects.aggregate(number_of_entries=Count('entry'))
  2. {'number_of_entries': 16}

For an in-depth discussion of aggregation, see the topic guide on Aggregation.

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) is a member of a QuerySet is:

  1. entry = Entry.objects.get(pk=123)
  2. if some_queryset.filter(pk=entry.pk).exists():
  3. print("Entry contained in queryset")

Which will be faster than the following which requires evaluating and iterating through the entire queryset:

  1. if entry in some_queryset:
  2. print("Entry contained in QuerySet")

And to find whether a queryset contains any items:

  1. if some_queryset.exists():
  2. print("There is at least one object in some_queryset")

Which will be faster than:

  1. if some_queryset:
  2. print("There is at least one object in some_queryset")

… but not by a large degree (hence needing a large queryset for efficiency gains).

Additionally, if a some_queryset has not yet been evaluated, but you know that it will be at some point, then using some_queryset.exists() will do more overall work (one query for the existence check plus an extra one to later retrieve the results) than using bool(some_queryset), which retrieves the results and then checks if any were returned.

update()

update(\*kwargs*)

Performs an SQL update query for the specified fields, and returns the number of rows matched (which may not be equal to the number of rows updated if some rows already have the new value).

For example, to turn comments off for all blog entries published in 2010, you could do this:

  1. >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)

(This assumes your Entry model has fields pub_date and comments_on.)

You can update multiple fields — there’s no limit on how many. For example, here we update the comments_on and headline fields:

  1. >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False, headline='This is old')

The update() method is applied instantly, and the only restriction on the QuerySet that is updated is that it can only update columns in the model’s main table, not on related models. You can’t do this, for example:

  1. >>> Entry.objects.update(blog__name='foo') # Won't work!

Filtering based on related fields is still possible, though:

  1. >>> Entry.objects.filter(blog__id=1).update(comments_on=True)

You cannot call update() on a QuerySet that has had a slice taken or can otherwise no longer be filtered.

The update() method returns the number of affected rows:

  1. >>> Entry.objects.filter(id=64).update(comments_on=True)
  2. 1
  3. >>> Entry.objects.filter(slug='nonexistent-slug').update(comments_on=True)
  4. 0
  5. >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)
  6. 132

If you’re just updating a record and don’t need to do anything with the model object, the most efficient approach is to call update(), rather than loading the model object into memory. For example, instead of doing this:

  1. e = Entry.objects.get(id=10)
  2. e.comments_on = False
  3. e.save()

…do this:

  1. Entry.objects.filter(id=10).update(comments_on=False)

Using update() also prevents a race condition wherein something might change in your database in the short period of time between loading the object and calling save().

Finally, realize that update() does an update at the SQL level and, thus, does not call any save() methods on your models, nor does it emit the pre_save or post_save signals (which are a consequence of calling Model.save()). If you want to update a bunch of records for a model that has a custom save() method, loop over them and call save(), like this:

  1. for e in Entry.objects.filter(pub_date__year=2010):
  2. e.comments_on = False
  3. e.save()

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:

  1. >>> b = Blog.objects.get(pk=1)
  2. # Delete all the entries belonging to this Blog.
  3. >>> Entry.objects.filter(blog=b).delete()
  4. (4, {'weblog.Entry': 2, 'weblog.Entry_authors': 2})

By default, Django’s ForeignKey emulates the SQL constraint ON DELETE CASCADE — in other words, any objects with foreign keys pointing at the objects to be deleted will be deleted along with them. For example:

  1. >>> blogs = Blog.objects.all()
  2. # This will delete all Blogs and all of their Entry objects.
  3. >>> blogs.delete()
  4. (5, {'weblog.Blog': 1, 'weblog.Entry': 2, 'weblog.Entry_authors': 2})

这种约束行为通过 ForeignKeyon_delete 参数定义。

The delete() method does a bulk delete and does not call any delete() methods on your models. It does, however, emit the pre_delete and post_delete signals for all deleted objects (including cascaded deletions).

Django needs to fetch objects into memory to send signals and handle cascades. However, if there are no cascades and no signals, then Django may take a fast-path and delete objects without fetching into memory. For large deletes this can result in significantly reduced memory usage. The amount of executed queries can be reduced, too.

ForeignKeys which are set to on_delete DO_NOTHING do not prevent taking the fast-path in deletion.

Note that the queries generated in object deletion is an implementation detail subject to change.

as_manager()

classmethod as_manager()

Class method that returns an instance of Manager with a copy of the QuerySet’s methods. See 创建带有 QuerySet 方法的管理器 for more details.

explain()

explain(format=None, \*options*)

Returns a string of the QuerySet’s execution plan, which details how the database would execute the query, including any indexes or joins that would be used. Knowing these details may help you improve the performance of slow queries.

For example, when using PostgreSQL:

  1. >>> print(Blog.objects.filter(title='My Blog').explain())
  2. Seq Scan on blog (cost=0.00..35.50 rows=10 width=12)
  3. Filter: (title = 'My Blog'::bpchar)

The output differs significantly between databases.

explain() is supported by all built-in database backends except Oracle because an implementation there isn’t straightforward.

The format parameter changes the output format from the databases’s default, usually text-based. PostgreSQL supports 'TEXT', 'JSON', 'YAML', and 'XML'. MySQL supports 'TEXT' (also called 'TRADITIONAL') and 'JSON'.

Some databases accept flags that can return more information about the query. Pass these flags as keyword arguments. For example, when using PostgreSQL:

  1. >>> print(Blog.objects.filter(title='My Blog').explain(verbose=True))
  2. Seq Scan on public.blog (cost=0.00..35.50 rows=10 width=12) (actual time=0.004..0.004 rows=10 loops=1)
  3. Output: id, title
  4. Filter: (blog.title = 'My Blog'::bpchar)
  5. Planning time: 0.064 ms
  6. Execution time: 0.058 ms

On some databases, flags may cause the query to be executed which could have adverse effects on your database. For example, PostgreSQL’s ANALYZE flag could result in changes to data if there are triggers or if a function is called, even for a SELECT query.

Field lookups

Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get().

For an introduction, see models and database queries documentation.

Django’s built-in lookups are listed below. It is also possible to write custom lookups for model fields.

As a convenience when no lookup type is provided (like in Entry.objects.get(id=14)) the lookup type is assumed to be exact.

exact

Exact match. If the value provided for comparison is None, it will be interpreted as an SQL NULL (see isnull for more details).

举例:

  1. Entry.objects.get(id__exact=14)
  2. Entry.objects.get(id__exact=None)

SQL equivalents:

  1. SELECT ... WHERE id = 14;
  2. SELECT ... WHERE id IS NULL;

MySQL comparisons

In MySQL, a database table’s “collation” setting determines whether exact comparisons are case-sensitive. This is a database setting, not a Django setting. It’s possible to configure your MySQL tables to use case-sensitive comparisons, but some trade-offs are involved. For more information about this, see the collation section in the databases documentation.

iexact

Case-insensitive exact match. If the value provided for comparison is None, it will be interpreted as an SQL NULL (see isnull for more details).

举例:

  1. Blog.objects.get(name__iexact='beatles blog')
  2. Blog.objects.get(name__iexact=None)

SQL equivalents:

  1. SELECT ... WHERE name ILIKE 'beatles blog';
  2. SELECT ... WHERE name IS NULL;

Note the first query will match 'Beatles Blog', 'beatles blog', 'BeAtLes BLoG', etc.

SQLite users

When using the SQLite backend and non-ASCII strings, bear in mind the database note about string comparisons. SQLite does not do case-insensitive matching for non-ASCII strings.

contains

Case-sensitive containment test.

举例:

  1. Entry.objects.get(headline__contains='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline LIKE '%Lennon%';

Note this will match the headline 'Lennon honored today' but not 'lennon honored today'.

SQLite users

SQLite doesn’t support case-sensitive LIKE statements; contains acts like icontains for SQLite. See the database note for more information.

icontains

Case-insensitive containment test.

举例:

  1. Entry.objects.get(headline__icontains='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline ILIKE '%Lennon%';

SQLite users

When using the SQLite backend and non-ASCII strings, bear in mind the database note about string comparisons.

in

In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted.

举例:

  1. Entry.objects.filter(id__in=[1, 3, 4])
  2. Entry.objects.filter(headline__in='abc')

SQL equivalents:

  1. SELECT ... WHERE id IN (1, 3, 4);
  2. SELECT ... WHERE headline IN ('a', 'b', 'c');

You can also use a queryset to dynamically evaluate the list of values instead of providing a list of literal values:

  1. inner_qs = Blog.objects.filter(name__contains='Cheddar')
  2. entries = Entry.objects.filter(blog__in=inner_qs)

This queryset will be evaluated as subselect statement:

  1. SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%Cheddar%')

If you pass in a QuerySet resulting from values() or values_list() as the value to an __in lookup, you need to ensure you are only extracting one field in the result. For example, this will work (filtering on the blog names):

  1. inner_qs = Blog.objects.filter(name__contains='Ch').values('name')
  2. entries = Entry.objects.filter(blog__name__in=inner_qs)

This example will raise an exception, since the inner query is trying to extract two field values, where only one is expected:

  1. # Bad code! Will raise a TypeError.
  2. inner_qs = Blog.objects.filter(name__contains='Ch').values('name', 'id')
  3. entries = Entry.objects.filter(blog__name__in=inner_qs)

性能考虑因素

Be cautious about using nested queries and understand your database server’s performance characteristics (if in doubt, benchmark!). Some database backends, most notably MySQL, don’t optimize nested queries very well. It is more efficient, in those cases, to extract a list of values and then pass that into the second query. That is, execute two queries instead of one:

  1. values = Blog.objects.filter(
  2. name__contains='Cheddar').values_list('pk', flat=True)
  3. entries = Entry.objects.filter(blog__in=list(values))

Note the list() call around the Blog QuerySet to force execution of the first query. Without it, a nested query would be executed, because QuerySet 是惰性的.

gt

Greater than.

举例:

  1. Entry.objects.filter(id__gt=4)

SQL equivalent:

  1. SELECT ... WHERE id > 4;

gte

Greater than or equal to.

lt

Less than.

lte

Less than or equal to.

startswith

Case-sensitive starts-with.

举例:

  1. Entry.objects.filter(headline__startswith='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline LIKE 'Lennon%';

SQLite doesn’t support case-sensitive LIKE statements; startswith acts like istartswith for SQLite.

istartswith

Case-insensitive starts-with.

举例:

  1. Entry.objects.filter(headline__istartswith='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline ILIKE 'Lennon%';

SQLite users

When using the SQLite backend and non-ASCII strings, bear in mind the database note about string comparisons.

endswith

Case-sensitive ends-with.

举例:

  1. Entry.objects.filter(headline__endswith='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline LIKE '%Lennon';

SQLite users

SQLite doesn’t support case-sensitive LIKE statements; endswith acts like iendswith for SQLite. Refer to the database note documentation for more.

iendswith

Case-insensitive ends-with.

举例:

  1. Entry.objects.filter(headline__iendswith='Lennon')

SQL equivalent:

  1. SELECT ... WHERE headline ILIKE '%Lennon'

SQLite users

When using the SQLite backend and non-ASCII strings, bear in mind the database note about string comparisons.

range

Range test (inclusive).

举例:

  1. import datetime
  2. start_date = datetime.date(2005, 1, 1)
  3. end_date = datetime.date(2005, 3, 31)
  4. Entry.objects.filter(pub_date__range=(start_date, end_date))

SQL equivalent:

  1. SELECT ... WHERE pub_date BETWEEN '2005-01-01' and '2005-03-31';

You can use range anywhere you can use BETWEEN in SQL — for dates, numbers and even characters.

警告

Filtering a DateTimeField with dates won’t include items on the last day, because the bounds are interpreted as “0am on the given date”. If pub_date was a DateTimeField, the above expression would be turned into this SQL:

  1. SELECT ... WHERE pub_date BETWEEN '2005-01-01 00:00:00' and '2005-03-31 00:00:00';

Generally speaking, you can’t mix dates and datetimes.

date

For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.

举例:

  1. Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
  2. Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))

(No equivalent SQL code fragment is included for this lookup because implementation of the relevant query varies among different database engines.)

When USE_TZ is True, fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

year

For date and datetime fields, an exact year match. Allows chaining additional field lookups. Takes an integer year.

举例:

  1. Entry.objects.filter(pub_date__year=2005)
  2. Entry.objects.filter(pub_date__year__gte=2005)

SQL equivalent:

  1. SELECT ... WHERE pub_date BETWEEN '2005-01-01' AND '2005-12-31';
  2. SELECT ... WHERE pub_date >= '2005-01-01';

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

iso_year

New in Django 2.2.

For date and datetime fields, an exact ISO 8601 week-numbering year match. Allows chaining additional field lookups. Takes an integer year.

举例:

  1. Entry.objects.filter(pub_date__iso_year=2005)
  2. Entry.objects.filter(pub_date__iso_year__gte=2005)

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

month

For date and datetime fields, an exact month match. Allows chaining additional field lookups. Takes an integer 1 (January) through 12 (December).

举例:

  1. Entry.objects.filter(pub_date__month=12)
  2. Entry.objects.filter(pub_date__month__gte=6)

SQL equivalent:

  1. SELECT ... WHERE EXTRACT('month' FROM pub_date) = '12';
  2. SELECT ... WHERE EXTRACT('month' FROM pub_date) >= '6';

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

day

For date and datetime fields, an exact day match. Allows chaining additional field lookups. Takes an integer day.

举例:

  1. Entry.objects.filter(pub_date__day=3)
  2. Entry.objects.filter(pub_date__day__gte=3)

SQL equivalent:

  1. SELECT ... WHERE EXTRACT('day' FROM pub_date) = '3';
  2. SELECT ... WHERE EXTRACT('day' FROM pub_date) >= '3';

(The exact SQL syntax varies for each database engine.)

Note this will match any record with a pub_date on the third day of the month, such as January 3, July 3, etc.

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

week

For date and datetime fields, return the week number (1-52 or 53) according to ISO-8601, i.e., weeks start on a Monday and the first week contains the year’s first Thursday.

举例:

  1. Entry.objects.filter(pub_date__week=52)
  2. Entry.objects.filter(pub_date__week__gte=32, pub_date__week__lte=38)

(No equivalent SQL code fragment is included for this lookup because implementation of the relevant query varies among different database engines.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

week_day

For date and datetime fields, a ‘day of the week’ match. Allows chaining additional field lookups.

Takes an integer value representing the day of week from 1 (Sunday) to 7 (Saturday).

举例:

  1. Entry.objects.filter(pub_date__week_day=2)
  2. Entry.objects.filter(pub_date__week_day__gte=2)

(No equivalent SQL code fragment is included for this lookup because implementation of the relevant query varies among different database engines.)

Note this will match any record with a pub_date that falls on a Monday (day 2 of the week), regardless of the month or year in which it occurs. Week days are indexed with day 1 being Sunday and day 7 being Saturday.

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

quarter

For date and datetime fields, a ‘quarter of the year’ match. Allows chaining additional field lookups. Takes an integer value between 1 and 4 representing the quarter of the year.

Example to retrieve entries in the second quarter (April 1 to June 30):

  1. Entry.objects.filter(pub_date__quarter=2)

(No equivalent SQL code fragment is included for this lookup because implementation of the relevant query varies among different database engines.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

time

For datetime fields, casts the value as time. Allows chaining additional field lookups. Takes a datetime.time value.

举例:

  1. Entry.objects.filter(pub_date__time=datetime.time(14, 30))
  2. Entry.objects.filter(pub_date__time__range=(datetime.time(8), datetime.time(17)))

(No equivalent SQL code fragment is included for this lookup because implementation of the relevant query varies among different database engines.)

When USE_TZ is True, fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

hour

For datetime and time fields, an exact hour match. Allows chaining additional field lookups. Takes an integer between 0 and 23.

举例:

  1. Event.objects.filter(timestamp__hour=23)
  2. Event.objects.filter(time__hour=5)
  3. Event.objects.filter(timestamp__hour__gte=12)

SQL equivalent:

  1. SELECT ... WHERE EXTRACT('hour' FROM timestamp) = '23';
  2. SELECT ... WHERE EXTRACT('hour' FROM time) = '5';
  3. SELECT ... WHERE EXTRACT('hour' FROM timestamp) >= '12';

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

minute

For datetime and time fields, an exact minute match. Allows chaining additional field lookups. Takes an integer between 0 and 59.

举例:

  1. Event.objects.filter(timestamp__minute=29)
  2. Event.objects.filter(time__minute=46)
  3. Event.objects.filter(timestamp__minute__gte=29)

SQL equivalent:

  1. SELECT ... WHERE EXTRACT('minute' FROM timestamp) = '29';
  2. SELECT ... WHERE EXTRACT('minute' FROM time) = '46';
  3. SELECT ... WHERE EXTRACT('minute' FROM timestamp) >= '29';

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

second

For datetime and time fields, an exact second match. Allows chaining additional field lookups. Takes an integer between 0 and 59.

举例:

  1. Event.objects.filter(timestamp__second=31)
  2. Event.objects.filter(time__second=2)
  3. Event.objects.filter(timestamp__second__gte=31)

SQL equivalent:

  1. SELECT ... WHERE EXTRACT('second' FROM timestamp) = '31';
  2. SELECT ... WHERE EXTRACT('second' FROM time) = '2';
  3. SELECT ... WHERE EXTRACT('second' FROM timestamp) >= '31';

(The exact SQL syntax varies for each database engine.)

When USE_TZ is True, datetime fields are converted to the current time zone before filtering. This requires time zone definitions in the database.

isnull

Takes either True or False, which correspond to SQL queries of IS NULL and IS NOT NULL, respectively.

举例:

  1. Entry.objects.filter(pub_date__isnull=True)

SQL equivalent:

  1. SELECT ... WHERE pub_date IS NULL;

regex

Case-sensitive regular expression match.

The regular expression syntax is that of the database backend in use. In the case of SQLite, which has no built in regular expression support, this feature is provided by a (Python) user-defined REGEXP function, and the regular expression syntax is therefore that of Python’s re module.

举例:

  1. Entry.objects.get(title__regex=r'^(An?|The) +')

SQL equivalents:

  1. SELECT ... WHERE title REGEXP BINARY '^(An?|The) +'; -- MySQL
  2. SELECT ... WHERE REGEXP_LIKE(title, '^(An?|The) +', 'c'); -- Oracle
  3. SELECT ... WHERE title ~ '^(An?|The) +'; -- PostgreSQL
  4. SELECT ... WHERE title REGEXP '^(An?|The) +'; -- SQLite

Using raw strings (e.g., r'foo' instead of 'foo') for passing in the regular expression syntax is recommended.

iregex

Case-insensitive regular expression match.

举例:

  1. Entry.objects.get(title__iregex=r'^(an?|the) +')

SQL equivalents:

  1. SELECT ... WHERE title REGEXP '^(an?|the) +'; -- MySQL
  2. SELECT ... WHERE REGEXP_LIKE(title, '^(an?|the) +', 'i'); -- Oracle
  3. SELECT ... WHERE title ~* '^(an?|the) +'; -- PostgreSQL
  4. SELECT ... WHERE title REGEXP '(?i)^(an?|the) +'; -- SQLite

Aggregation functions

Django provides the following aggregation functions in the django.db.models module. For details on how to use these aggregate functions, see the topic guide on aggregation. See the Aggregate documentation to learn how to create your aggregates.

警告

SQLite can’t handle aggregation on date/time fields out of the box. This is because there are no native date/time fields in SQLite and Django currently emulates these features using a text field. Attempts to use aggregation on date/time fields in SQLite will raise NotImplementedError.

注释

Aggregation functions return None when used with an empty QuerySet. For example, the Sum aggregation function returns None instead of 0 if the QuerySet contains no entries. An exception is Count, which does return 0 if the QuerySet is empty.

All aggregates have the following parameters in common:

expressions

Strings that reference fields on the model, or query expressions.

output_field

An optional argument that represents the model field of the return value

注解

When combining multiple field types, Django can only determine the output_field if all fields are of the same type. Otherwise, you must provide the output_field yourself.

filter

An optional Q object that’s used to filter the rows that are aggregated.

See Conditional aggregation and 过滤注解 for example usage.

**extra

Keyword arguments that can provide extra context for the SQL generated by the aggregate.

Avg

class Avg(expression, output_field=None, distinct=False, filter=None, \*extra*)

Returns the mean value of the given expression, which must be numeric unless you specify a different output_field.

  • Default alias: <field>__avg
  • Return type: float if input is int, otherwise same as input field, or output_field if supplied

包含一个可选参数:

  • distinct

    If distinct=True, Avg returns the mean value of unique values. This is the SQL equivalent of AVG(DISTINCT <field>). The default value is False.

Changed in Django 3.0:

Support for distinct=True was added.

Count

class Count(expression, distinct=False, filter=None, \*extra*)

Returns the number of objects that are related through the provided expression.

  • Default alias: <field>__count
  • Return type: int

包含一个可选参数:

  • distinct

    If distinct=True, the count will only include unique instances. This is the SQL equivalent of COUNT(DISTINCT <field>). The default value is False.

Max

class Max(expression, output_field=None, filter=None, \*extra*)

Returns the maximum value of the given expression.

  • Default alias: <field>__max
  • Return type: same as input field, or output_field if supplied

Min

class Min(expression, output_field=None, filter=None, \*extra*)

Returns the minimum value of the given expression.

  • Default alias: <field>__min
  • Return type: same as input field, or output_field if supplied

StdDev

class StdDev(expression, output_field=None, sample=False, filter=None, \*extra*)

Returns the standard deviation of the data in the provided expression.

  • Default alias: <field>__stddev
  • Return type: float if input is int, otherwise same as input field, or output_field if supplied

包含一个可选参数:

  • sample

    By default, StdDev returns the population standard deviation. However, if sample=True, the return value will be the sample standard deviation.

Changed in Django 2.2:

SQLite support was added.

Sum

class Sum(expression, output_field=None, distinct=False, filter=None, \*extra*)

Computes the sum of all values of the given expression.

  • Default alias: <field>__sum
  • Return type: same as input field, or output_field if supplied

包含一个可选参数:

  • distinct

    If distinct=True, Sum returns the sum of unique values. This is the SQL equivalent of SUM(DISTINCT <field>). The default value is False.

Changed in Django 3.0:

Support for distinct=True was added.

Variance

class Variance(expression, output_field=None, sample=False, filter=None, \*extra*)

Returns the variance of the data in the provided expression.

  • Default alias: <field>__variance
  • Return type: float if input is int, otherwise same as input field, or output_field if supplied

包含一个可选参数:

  • sample

    By default, Variance returns the population variance. However, if sample=True, the return value will be the sample variance.

Changed in Django 2.2:

SQLite support was added.

Query-related tools

This section provides reference material for query-related tools not documented elsewhere.

Q() objects

class Q

A Q() object, like an F object, encapsulates a SQL expression in a Python object that can be used in database-related operations.

In general, Q() objects make it possible to define and reuse conditions. This permits the construction of complex database queries using | (OR) and & (AND) operators; in particular, it is not otherwise possible to use OR in QuerySets.

Prefetch() objects

class Prefetch(lookup, queryset=None, to_attr=None)

The Prefetch() object can be used to control the operation of prefetch_related().

The lookup argument describes the relations to follow and works the same as the string based lookups passed to prefetch_related(). For example:

  1. >>> from django.db.models import Prefetch
  2. >>> Question.objects.prefetch_related(Prefetch('choice_set')).get().choice_set.all()
  3. <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
  4. # This will only execute two queries regardless of the number of Question
  5. # and Choice objects.
  6. >>> Question.objects.prefetch_related(Prefetch('choice_set')).all()
  7. <QuerySet [<Question: What's up?>]>

The queryset argument supplies a base QuerySet for the given lookup. This is useful to further filter down the prefetch operation, or to call select_related() from the prefetched relation, hence reducing the number of queries even further:

  1. >>> voted_choices = Choice.objects.filter(votes__gt=0)
  2. >>> voted_choices
  3. <QuerySet [<Choice: The sky>]>
  4. >>> prefetch = Prefetch('choice_set', queryset=voted_choices)
  5. >>> Question.objects.prefetch_related(prefetch).get().choice_set.all()
  6. <QuerySet [<Choice: The sky>]>

The to_attr argument sets the result of the prefetch operation to a custom attribute:

  1. >>> prefetch = Prefetch('choice_set', queryset=voted_choices, to_attr='voted_choices')
  2. >>> Question.objects.prefetch_related(prefetch).get().voted_choices
  3. [<Choice: The sky>]
  4. >>> Question.objects.prefetch_related(prefetch).get().choice_set.all()
  5. <QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>

注解

When using to_attr the prefetched result is stored in a list. This can provide a significant speed improvement over traditional prefetch_related calls which store the cached result within a QuerySet instance.

prefetch_related_objects()

prefetch_related_objects(model_instances, \related_lookups*)

Prefetches the given lookups on an iterable of model instances. This is useful in code that receives a list of model instances as opposed to a QuerySet; for example, when fetching models from a cache or instantiating them manually.

Pass an iterable of model instances (must all be of the same class) and the lookups or Prefetch objects you want to prefetch for. For example:

  1. >>> from django.db.models import prefetch_related_objects
  2. >>> restaurants = fetch_top_restaurants_from_cache() # A list of Restaurants
  3. >>> prefetch_related_objects(restaurants, 'pizzas__toppings')

FilteredRelation() objects

class FilteredRelation(relation_name, **, condition=Q()*)

  • relation_name

    The name of the field on which you’d like to filter the relation.

  • condition

    A Q object to control the filtering.

FilteredRelation is used with annotate() to create an ON clause when a JOIN is performed. It doesn’t act on the default relationship but on the annotation name (pizzas_vegetarian in example below).

For example, to find restaurants that have vegetarian pizzas with 'mozzarella' in the name:

  1. >>> from django.db.models import FilteredRelation, Q
  2. >>> Restaurant.objects.annotate(
  3. ... pizzas_vegetarian=FilteredRelation(
  4. ... 'pizzas', condition=Q(pizzas__vegetarian=True),
  5. ... ),
  6. ... ).filter(pizzas_vegetarian__name__icontains='mozzarella')

If there are a large number of pizzas, this queryset performs better than:

  1. >>> Restaurant.objects.filter(
  2. ... pizzas__vegetarian=True,
  3. ... pizzas__name__icontains='mozzarella',
  4. ... )

because the filtering in the WHERE clause of the first queryset will only operate on vegetarian pizzas.

FilteredRelation doesn’t support:

  • Conditions that span relational fields. For example:

    1. >>> Restaurant.objects.annotate(
    2. ... pizzas_with_toppings_startswith_n=FilteredRelation(
    3. ... 'pizzas__toppings',
    4. ... condition=Q(pizzas__toppings__name__startswith='n'),
    5. ... ),
    6. ... )
    7. Traceback (most recent call last):
    8. ...
    9. ValueError: FilteredRelation's condition doesn't support nested relations (got 'pizzas__toppings__name__startswith').
  • QuerySet.only() and prefetch_related().

  • A GenericForeignKey inherited from a parent model.