5 .. currentmodule:: django.db.models.query
7 This document describes the details of the ``QuerySet`` API. It builds on the
8 material presented in the :doc:`model </topics/db/models>` and :doc:`database
9 query </topics/db/queries>` guides, so you'll probably want to read and
10 understand those documents before reading this one.
12 Throughout this reference we'll use the :ref:`example Weblog models
13 <queryset-model-example>` presented in the :doc:`database query guide
14 </topics/db/queries>`.
16 .. _when-querysets-are-evaluated:
18 When QuerySets are evaluated
19 ============================
21 Internally, a ``QuerySet`` can be constructed, filtered, sliced, and generally
22 passed around without actually hitting the database. No database activity
23 actually occurs until you do something to evaluate the queryset.
25 You can evaluate a ``QuerySet`` in the following ways:
27 * **Iteration.** A ``QuerySet`` is iterable, and it executes its database
28 query the first time you iterate over it. For example, this will print
29 the headline of all entries in the database::
31 for e in Entry.objects.all():
34 * **Slicing.** As explained in :ref:`limiting-querysets`, a ``QuerySet`` can
35 be sliced, using Python's array-slicing syntax. Slicing an unevaluated
36 ``QuerySet`` usually returns another unevaluated ``QuerySet``, but Django
37 will execute the database query if you use the "step" parameter of slice
38 syntax, and will return a list. Slicing a ``QuerySet`` that has been
39 evaluated (partially or fully) also returns a list.
41 * **Pickling/Caching.** See the following section for details of what
42 is involved when `pickling QuerySets`_. The important thing for the
43 purposes of this section is that the results are read from the database.
45 * **repr().** A ``QuerySet`` is evaluated when you call ``repr()`` on it.
46 This is for convenience in the Python interactive interpreter, so you can
47 immediately see your results when using the API interactively.
49 * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it.
50 This, as you might expect, returns the length of the result list.
52 Note: *Don't* use ``len()`` on ``QuerySet``\s if all you want to do is
53 determine the number of records in the set. It's much more efficient to
54 handle a count at the database level, using SQL's ``SELECT COUNT(*)``,
55 and Django provides a ``count()`` method for precisely this reason. See
58 * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on
61 entry_list = list(Entry.objects.all())
63 Be warned, though, that this could have a large memory overhead, because
64 Django will load each element of the list into memory. In contrast,
65 iterating over a ``QuerySet`` will take advantage of your database to
66 load data and instantiate objects only as you need them.
68 * **bool().** Testing a ``QuerySet`` in a boolean context, such as using
69 ``bool()``, ``or``, ``and`` or an ``if`` statement, will cause the query
70 to be executed. If there is at least one result, the ``QuerySet`` is
71 ``True``, otherwise ``False``. For example::
73 if Entry.objects.filter(headline="Test"):
74 print "There is at least one Entry with the headline Test"
76 Note: *Don't* use this if all you want to do is determine if at least one
77 result exists, and don't need the actual objects. It's more efficient to
78 use :meth:`exists() <QuerySet.exists>` (see below).
80 .. _pickling QuerySets:
85 If you :mod:`pickle` a ``QuerySet``, this will force all the results to be loaded
86 into memory prior to pickling. Pickling is usually used as a precursor to
87 caching and when the cached queryset is reloaded, you want the results to
88 already be present and ready for use (reading from the database can take some
89 time, defeating the purpose of caching). This means that when you unpickle a
90 ``QuerySet``, it contains the results at the moment it was pickled, rather
91 than the results that are currently in the database.
93 If you only want to pickle the necessary information to recreate the
94 ``QuerySet`` from the database at a later time, pickle the ``query`` attribute
95 of the ``QuerySet``. You can then recreate the original ``QuerySet`` (without
96 any results loaded) using some code like this::
99 >>> query = pickle.loads(s) # Assuming 's' is the pickled string.
100 >>> qs = MyModel.objects.all()
101 >>> qs.query = query # Restore the original 'query'.
103 The ``query`` attribute is an opaque object. It represents the internals of
104 the query construction and is not part of the public API. However, it is safe
105 (and fully supported) to pickle and unpickle the attribute's contents as
108 .. admonition:: You can't share pickles between versions
110 Pickles of QuerySets are only valid for the version of Django that
111 was used to generate them. If you generate a pickle using Django
112 version N, there is no guarantee that pickle will be readable with
113 Django version N+1. Pickles should not be used as part of a long-term
121 Though you usually won't create one manually — you'll go through a
122 :class:`~django.db.models.Manager` — here's the formal declaration of a
125 .. class:: QuerySet([model=None, query=None, using=None])
127 Usually when you'll interact with a ``QuerySet`` you'll use it by
128 :ref:`chaining filters <chaining-filters>`. To make this work, most
129 ``QuerySet`` methods return new querysets. These methods are covered in
130 detail later in this section.
132 The ``QuerySet`` class has two public attributes you can use for
135 .. attribute:: ordered
137 ``True`` if the ``QuerySet`` is ordered — i.e. has an
138 :meth:`order_by()` clause or a default ordering on the model.
143 The database that will be used if this query is executed now.
147 The ``query`` parameter to :class:`QuerySet` exists so that specialized
148 query subclasses such as
149 :class:`~django.contrib.gis.db.models.GeoQuerySet` can reconstruct
150 internal query state. The value of the parameter is an opaque
151 representation of that query state and is not part of a public API.
152 To put it simply: if you need to ask, you don't need to use it.
154 .. currentmodule:: django.db.models.query.QuerySet
156 Methods that return new QuerySets
157 ---------------------------------
159 Django provides a range of ``QuerySet`` refinement methods that modify either
160 the types of results returned by the ``QuerySet`` or the way its SQL query is
166 .. method:: filter(**kwargs)
168 Returns a new ``QuerySet`` containing objects that match the given lookup
171 The lookup parameters (``**kwargs``) should be in the format described in
172 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
173 underlying SQL statement.
178 .. method:: exclude(**kwargs)
180 Returns a new ``QuerySet`` containing objects that do *not* match the given
183 The lookup parameters (``**kwargs``) should be in the format described in
184 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
185 underlying SQL statement, and the whole thing is enclosed in a ``NOT()``.
187 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
188 AND whose ``headline`` is "Hello"::
190 Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')
192 In SQL terms, that evaluates to::
195 WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello')
197 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
198 OR whose headline is "Hello"::
200 Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello')
202 In SQL terms, that evaluates to::
205 WHERE NOT pub_date > '2005-1-3'
206 AND NOT headline = 'Hello'
208 Note the second example is more restrictive.
213 .. method:: annotate(*args, **kwargs)
215 Annotates each object in the ``QuerySet`` with the provided list of
216 aggregate values (averages, sums, etc) that have been computed over
217 the objects that are related to the objects in the ``QuerySet``.
218 Each argument to ``annotate()`` is an annotation that will be added
219 to each object in the ``QuerySet`` that is returned.
221 The aggregation functions that are provided by Django are described
222 in `Aggregation Functions`_ below.
224 Annotations specified using keyword arguments will use the keyword as
225 the alias for the annotation. Anonymous arguments will have an alias
226 generated for them based upon the name of the aggregate function and
227 the model field that is being aggregated.
229 For example, if you were manipulating a list of blogs, you may want
230 to determine how many entries have been made in each blog::
232 >>> q = Blog.objects.annotate(Count('entry'))
233 # The name of the first blog
236 # The number of entries on the first blog
237 >>> q[0].entry__count
240 The ``Blog`` model doesn't define an ``entry__count`` attribute by itself,
241 but by using a keyword argument to specify the aggregate function, you can
242 control the name of the annotation::
244 >>> q = Blog.objects.annotate(number_of_entries=Count('entry'))
245 # The number of entries on the first blog, using the name provided
246 >>> q[0].number_of_entries
249 For an in-depth discussion of aggregation, see :doc:`the topic guide on
250 Aggregation </topics/db/aggregation>`.
255 .. method:: order_by(*fields)
257 By default, results returned by a ``QuerySet`` are ordered by the ordering
258 tuple given by the ``ordering`` option in the model's ``Meta``. You can
259 override this on a per-``QuerySet`` basis by using the ``order_by`` method.
263 Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')
265 The result above will be ordered by ``pub_date`` descending, then by
266 ``headline`` ascending. The negative sign in front of ``"-pub_date"`` indicates
267 *descending* order. Ascending order is implied. To order randomly, use ``"?"``,
270 Entry.objects.order_by('?')
272 Note: ``order_by('?')`` queries may be expensive and slow, depending on the
273 database backend you're using.
275 To order by a field in a different model, use the same syntax as when you are
276 querying across model relations. That is, the name of the field, followed by a
277 double underscore (``__``), followed by the name of the field in the new model,
278 and so on for as many models as you want to join. For example::
280 Entry.objects.order_by('blog__name', 'headline')
282 If you try to order by a field that is a relation to another model, Django will
283 use the default ordering on the related model (or order by the related model's
284 primary key if there is no :attr:`Meta.ordering
285 <django.db.models.Options.ordering>` specified. For example::
287 Entry.objects.order_by('blog')
291 Entry.objects.order_by('blog__id')
293 ...since the ``Blog`` model has no default ordering specified.
295 Be cautious when ordering by fields in related models if you are also using
296 :meth:`distinct()`. See the note in :meth:`distinct` for an explanation of how
297 related model ordering can change the expected results.
299 It is permissible to specify a multi-valued field to order the results by (for
300 example, a :class:`~django.db.models.ManyToManyField` field). Normally
301 this won't be a sensible thing to do and it's really an advanced usage
302 feature. However, if you know that your queryset's filtering or available data
303 implies that there will only be one ordering piece of data for each of the main
304 items you are selecting, the ordering may well be exactly what you want to do.
305 Use ordering on multi-valued fields with care and make sure the results are
308 There's no way to specify whether ordering should be case sensitive. With
309 respect to case-sensitivity, Django will order results however your database
310 backend normally orders them.
312 If you don't want any ordering to be applied to a query, not even the default
313 ordering, call :meth:`order_by()` with no parameters.
315 You can tell if a query is ordered or not by checking the
316 :attr:`.QuerySet.ordered` attribute, which will be ``True`` if the
317 ``QuerySet`` has been ordered in any way.
322 .. method:: reverse()
324 Use the ``reverse()`` method to reverse the order in which a queryset's
325 elements are returned. Calling ``reverse()`` a second time restores the
326 ordering back to the normal direction.
328 To retrieve the ''last'' five items in a queryset, you could do this::
330 my_queryset.reverse()[:5]
332 Note that this is not quite the same as slicing from the end of a sequence in
333 Python. The above example will return the last item first, then the
334 penultimate item and so on. If we had a Python sequence and looked at
335 ``seq[-5:]``, we would see the fifth-last item first. Django doesn't support
336 that mode of access (slicing from the end), because it's not possible to do it
339 Also, note that ``reverse()`` should generally only be called on a ``QuerySet``
340 which has a defined ordering (e.g., when querying against a model which defines
341 a default ordering, or when using :meth:`order_by()`). If no such ordering is
342 defined for a given ``QuerySet``, calling ``reverse()`` on it has no real
343 effect (the ordering was undefined prior to calling ``reverse()``, and will
344 remain undefined afterward).
349 .. method:: distinct([*fields])
351 Returns a new ``QuerySet`` that uses ``SELECT DISTINCT`` in its SQL query. This
352 eliminates duplicate rows from the query results.
354 By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this
355 is rarely a problem, because simple queries such as ``Blog.objects.all()``
356 don't introduce the possibility of duplicate result rows. However, if your
357 query spans multiple tables, it's possible to get duplicate results when a
358 ``QuerySet`` is evaluated. That's when you'd use ``distinct()``.
361 Any fields used in an :meth:`order_by` call are included in the SQL
362 ``SELECT`` columns. This can sometimes lead to unexpected results when used
363 in conjunction with ``distinct()``. If you order by fields from a related
364 model, those fields will be added to the selected columns and they may make
365 otherwise duplicate rows appear to be distinct. Since the extra columns
366 don't appear in the returned results (they are only there to support
367 ordering), it sometimes looks like non-distinct results are being returned.
369 Similarly, if you use a :meth:`values()` query to restrict the columns
370 selected, the columns used in any :meth:`order_by()` (or default model
371 ordering) will still be involved and may affect uniqueness of the results.
373 The moral here is that if you are using ``distinct()`` be careful about
374 ordering by related models. Similarly, when using ``distinct()`` and
375 :meth:`values()` together, be careful when ordering by fields not in the
376 :meth:`values()` call.
378 .. versionadded:: 1.4
380 As of Django 1.4, you can pass positional arguments (``*fields``) in order to
381 specify the names of fields to which the ``DISTINCT`` should apply. This
382 translates to a ``SELECT DISTINCT ON`` SQL query.
384 Here's the difference. For a normal ``distinct()`` call, the database compares
385 *each* field in each row when determining which rows are distinct. For a
386 ``distinct()`` call with specified field names, the database will only compare
387 the specified field names.
390 This ability to specify field names is only available in PostgreSQL.
393 When you specify field names, you *must* provide an ``order_by()`` in the
394 QuerySet, and the fields in ``order_by()`` must start with the fields in
395 ``distinct()``, in the same order.
397 For example, ``SELECT DISTINCT ON (a)`` gives you the first row for each
398 value in column ``a``. If you don't specify an order, you'll get some
403 >>> Author.objects.distinct()
406 >>> Entry.objects.order_by('pub_date').distinct('pub_date')
409 >>> Entry.objects.order_by('blog').distinct('blog')
412 >>> Entry.objects.order_by('author', 'pub_date').distinct('author', 'pub_date')
415 >>> Entry.objects.order_by('blog__name', 'mod_date').distinct('blog__name', 'mod_date')
418 >>> Entry.objects.order_by('author', 'pub_date').distinct('author')
424 .. method:: values(*fields)
426 Returns a ``ValuesQuerySet`` — a ``QuerySet`` subclass that returns
427 dictionaries when used as an iterable, rather than model-instance objects.
429 Each of those dictionaries represents an object, with the keys corresponding to
430 the attribute names of model objects.
432 This example compares the dictionaries of ``values()`` with the normal model
435 # This list contains a Blog object.
436 >>> Blog.objects.filter(name__startswith='Beatles')
437 [<Blog: Beatles Blog>]
439 # This list contains a dictionary.
440 >>> Blog.objects.filter(name__startswith='Beatles').values()
441 [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]
443 The ``values()`` method takes optional positional arguments, ``*fields``, which
444 specify field names to which the ``SELECT`` should be limited. If you specify
445 the fields, each dictionary will contain only the field keys/values for the
446 fields you specify. If you don't specify the fields, each dictionary will
447 contain a key and value for every field in the database table.
451 >>> Blog.objects.values()
452 [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
453 >>> Blog.objects.values('id', 'name')
454 [{'id': 1, 'name': 'Beatles Blog'}]
456 A few subtleties that are worth mentioning:
458 * If you have a field called ``foo`` that is a
459 :class:`~django.db.models.ForeignKey`, the default ``values()`` call
460 will return a dictionary key called ``foo_id``, since this is the name
461 of the hidden model attribute that stores the actual value (the ``foo``
462 attribute refers to the related model). When you are calling
463 ``values()`` and passing in field names, you can pass in either ``foo``
464 or ``foo_id`` and you will get back the same thing (the dictionary key
465 will match the field name you passed in).
469 >>> Entry.objects.values()
470 [{'blog_id': 1, 'headline': u'First Entry', ...}, ...]
472 >>> Entry.objects.values('blog')
475 >>> Entry.objects.values('blog_id')
476 [{'blog_id': 1}, ...]
478 * When using ``values()`` together with :meth:`distinct()`, be aware that
479 ordering can affect the results. See the note in :meth:`distinct` for
482 * If you use a ``values()`` clause after an :meth:`extra()` call,
483 any fields defined by a ``select`` argument in the :meth:`extra()` must
484 be explicitly included in the ``values()`` call. Any :meth:`extra()` call
485 made after a ``values()`` call will have its extra selected fields
488 A ``ValuesQuerySet`` is useful when you know you're only going to need values
489 from a small number of the available fields and you won't need the
490 functionality of a model instance object. It's more efficient to select only
491 the fields you need to use.
493 Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all
494 methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or
495 whatever. Yes, that means these two calls are identical::
497 Blog.objects.values().order_by('id')
498 Blog.objects.order_by('id').values()
500 The people who made Django prefer to put all the SQL-affecting methods first,
501 followed (optionally) by any output-affecting methods (such as ``values()``),
502 but it doesn't really matter. This is your chance to really flaunt your
505 .. versionchanged:: 1.3
507 The ``values()`` method previously did not return anything for
508 :class:`~django.db.models.ManyToManyField` attributes and would raise an error
509 if you tried to pass this type of field to it.
511 This restriction has been lifted, and you can now also refer to fields on
512 related models with reverse relations through ``OneToOneField``, ``ForeignKey``
513 and ``ManyToManyField`` attributes::
515 Blog.objects.values('name', 'entry__headline')
516 [{'name': 'My blog', 'entry__headline': 'An entry'},
517 {'name': 'My blog', 'entry__headline': 'Another entry'}, ...]
521 Because :class:`~django.db.models.ManyToManyField` attributes and reverse
522 relations can have multiple related rows, including these can have a
523 multiplier effect on the size of your result set. This will be especially
524 pronounced if you include multiple such fields in your ``values()`` query,
525 in which case all possible combinations will be returned.
530 .. method:: values_list(*fields)
532 This is similar to ``values()`` except that instead of returning dictionaries,
533 it returns tuples when iterated over. Each tuple contains the value from the
534 respective field passed into the ``values_list()`` call — so the first item is
535 the first field, etc. For example::
537 >>> Entry.objects.values_list('id', 'headline')
538 [(1, u'First entry'), ...]
540 If you only pass in a single field, you can also pass in the ``flat``
541 parameter. If ``True``, this will mean the returned results are single values,
542 rather than one-tuples. An example should make the difference clearer::
544 >>> Entry.objects.values_list('id').order_by('id')
545 [(1,), (2,), (3,), ...]
547 >>> Entry.objects.values_list('id', flat=True).order_by('id')
550 It is an error to pass in ``flat`` when there is more than one field.
552 If you don't pass any values to ``values_list()``, it will return all the
553 fields in the model, in the order they were declared.
558 .. method:: dates(field, kind, order='ASC')
560 Returns a ``DateQuerySet`` — a ``QuerySet`` that evaluates to a list of
561 ``datetime.datetime`` objects representing all available dates of a particular
562 kind within the contents of the ``QuerySet``.
564 ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your
567 ``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each
568 ``datetime.datetime`` object in the result list is "truncated" to the given
571 * ``"year"`` returns a list of all distinct year values for the field.
572 * ``"month"`` returns a list of all distinct year/month values for the
574 * ``"day"`` returns a list of all distinct year/month/day values for the
577 ``order``, which defaults to ``'ASC'``, should be either ``'ASC'`` or
578 ``'DESC'``. This specifies how to order the results.
582 >>> Entry.objects.dates('pub_date', 'year')
583 [datetime.datetime(2005, 1, 1)]
584 >>> Entry.objects.dates('pub_date', 'month')
585 [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)]
586 >>> Entry.objects.dates('pub_date', 'day')
587 [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)]
588 >>> Entry.objects.dates('pub_date', 'day', order='DESC')
589 [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)]
590 >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day')
591 [datetime.datetime(2005, 3, 20)]
595 When :doc:`time zone support </topics/i18n/timezones>` is enabled, Django
596 uses UTC in the database connection, which means the aggregation is
597 performed in UTC. This is a known limitation of the current implementation.
604 Returns an ``EmptyQuerySet`` — a ``QuerySet`` subclass that always evaluates to
605 an empty list. This can be used in cases where you know that you should return
606 an empty result set and your caller is expecting a ``QuerySet`` object (instead
607 of returning an empty list, for example.)
611 >>> Entry.objects.none()
619 Returns a *copy* of the current ``QuerySet`` (or ``QuerySet`` subclass). This
620 can be useful in situations where you might want to pass in either a model
621 manager or a ``QuerySet`` and do further filtering on the result. After calling
622 ``all()`` on either object, you'll definitely have a ``QuerySet`` to work with.
627 .. method:: select_related()
629 Returns a ``QuerySet`` that will automatically "follow" foreign-key
630 relationships, selecting that additional related-object data when it executes
631 its query. This is a performance booster which results in (sometimes much)
632 larger queries but means later use of foreign-key relationships won't require
635 The following examples illustrate the difference between plain lookups and
636 ``select_related()`` lookups. Here's standard lookup::
639 e = Entry.objects.get(id=5)
641 # Hits the database again to get the related Blog object.
644 And here's ``select_related`` lookup::
647 e = Entry.objects.select_related().get(id=5)
649 # Doesn't hit the database, because e.blog has been prepopulated
650 # in the previous query.
653 ``select_related()`` follows foreign keys as far as possible. If you have the
656 class City(models.Model):
660 class Person(models.Model):
662 hometown = models.ForeignKey(City)
664 class Book(models.Model):
666 author = models.ForeignKey(Person)
668 ...then a call to ``Book.objects.select_related().get(id=4)`` will cache the
669 related ``Person`` *and* the related ``City``::
671 b = Book.objects.select_related().get(id=4)
672 p = b.author # Doesn't hit the database.
673 c = p.hometown # Doesn't hit the database.
675 b = Book.objects.get(id=4) # No select_related() in this example.
676 p = b.author # Hits the database.
677 c = p.hometown # Hits the database.
679 Note that, by default, ``select_related()`` does not follow foreign keys that
682 Usually, using ``select_related()`` can vastly improve performance because your
683 app can avoid many database calls. However, in situations with deeply nested
684 sets of relationships ``select_related()`` can sometimes end up following "too
685 many" relations, and can generate queries so large that they end up being slow.
687 In these situations, you can use the ``depth`` argument to ``select_related()``
688 to control how many "levels" of relations ``select_related()`` will actually
691 b = Book.objects.select_related(depth=1).get(id=4)
692 p = b.author # Doesn't hit the database.
693 c = p.hometown # Requires a database call.
695 Sometimes you only want to access specific models that are related to your root
696 model, not all of the related models. In these cases, you can pass the related
697 field names to ``select_related()`` and it will only follow those relations.
698 You can even do this for models that are more than one relation away by
699 separating the field names with double underscores, just as for filters. For
700 example, if you have this model::
702 class Room(models.Model):
704 building = models.ForeignKey(...)
706 class Group(models.Model):
708 teacher = models.ForeignKey(...)
709 room = models.ForeignKey(Room)
710 subject = models.ForeignKey(...)
712 ...and you only needed to work with the ``room`` and ``subject`` attributes,
713 you could write this::
715 g = Group.objects.select_related('room', 'subject')
719 g = Group.objects.select_related('room__building', 'subject')
721 ...and would also pull in the ``building`` relation.
723 You can refer to any :class:`~django.db.models.ForeignKey` or
724 :class:`~django.db.models.OneToOneField` relation in the list of fields
725 passed to ``select_related()``. This includes foreign keys that have
726 ``null=True`` (which are omitted in a no-parameter ``select_related()`` call).
727 It's an error to use both a list of fields and the ``depth`` parameter in the
728 same ``select_related()`` call; they are conflicting options.
730 .. versionchanged:: 1.2
732 You can also refer to the reverse direction of a
733 :class:`~django.db.models.OneToOneField` in the list of fields passed to
734 ``select_related`` — that is, you can traverse a
735 :class:`~django.db.models.OneToOneField` back to the object on which the field
736 is defined. Instead of specifying the field name, use the :attr:`related_name
737 <django.db.models.ForeignKey.related_name>` for the field on the related object.
739 A :class:`~django.db.models.OneToOneField` is not traversed in the reverse
740 direction if you are performing a depth-based ``select_related()`` call.
745 .. method:: prefetch_related(*lookups)
747 .. versionadded:: 1.4
749 Returns a ``QuerySet`` that will automatically retrieve, in a single batch,
750 related objects for each of the specified lookups.
752 This has a similar purpose to ``select_related``, in that both are designed to
753 stop the deluge of database queries that is caused by accessing related objects,
754 but the strategy is quite different.
756 ``select_related`` works by creating a SQL join and including the fields of the
757 related object in the SELECT statement. For this reason, ``select_related`` gets
758 the related objects in the same database query. However, to avoid the much
759 larger result set that would result from joining across a 'many' relationship,
760 ``select_related`` is limited to single-valued relationships - foreign key and
763 ``prefetch_related``, on the other hand, does a separate lookup for each
764 relationship, and does the 'joining' in Python. This allows it to prefetch
765 many-to-many and many-to-one objects, which cannot be done using
766 ``select_related``, in addition to the foreign key and one-to-one relationships
767 that are supported by ``select_related``. It also supports prefetching of
768 :class:`~django.contrib.contenttypes.generic.GenericRelation` and
769 :class:`~django.contrib.contenttypes.generic.GenericForeignKey`.
771 For example, suppose you have these models::
773 class Topping(models.Model):
774 name = models.CharField(max_length=30)
776 class Pizza(models.Model):
777 name = models.CharField(max_length=50)
778 toppings = models.ManyToManyField(Topping)
780 def __unicode__(self):
781 return u"%s (%s)" % (self.name, u", ".join([topping.name
782 for topping in self.toppings.all()]))
786 >>> Pizza.objects.all()
787 [u"Hawaiian (ham, pineapple)", u"Seafood (prawns, smoked salmon)"...
789 The problem with this code is that it will run a query on the Toppings table for
790 **every** item in the Pizza ``QuerySet``. Using ``prefetch_related``, this can
793 >>> Pizza.objects.all().prefetch_related('toppings')
795 All the relevant toppings will be fetched in a single query, and used to make
796 ``QuerySets`` that have a pre-filled cache of the relevant results. These
797 ``QuerySets`` are then used in the ``self.toppings.all()`` calls.
799 The additional queries are executed after the QuerySet has begun to be evaluated
800 and the primary query has been executed. Note that the result cache of the
801 primary QuerySet and all specified related objects will then be fully loaded
802 into memory, which is often avoided in other cases - even after a query has been
803 executed in the database, QuerySet normally tries to make uses of chunking
804 between the database to avoid loading all objects into memory before you need
807 Also remember that, as always with QuerySets, any subsequent chained methods
808 which imply a different database query will ignore previously cached results,
809 and retrieve data using a fresh database query. So, if you write the following:
811 >>> pizzas = Pizza.objects.prefetch_related('toppings')
812 >>> [list(pizza.toppings.filter(spicy=True)) for pizza in pizzas]
814 ...then the fact that ``pizza.toppings.all()`` has been prefetched will not help
815 you - in fact it hurts performance, since you have done a database query that
816 you haven't used. So use this feature with caution!
818 You can also use the normal join syntax to do related fields of related
819 fields. Suppose we have an additional model to the example above::
821 class Restaurant(models.Model):
822 pizzas = models.ManyToMany(Pizza, related_name='restaurants')
823 best_pizza = models.ForeignKey(Pizza, related_name='championed_by')
825 The following are all legal:
827 >>> Restaurant.objects.prefetch_related('pizzas__toppings')
829 This will prefetch all pizzas belonging to restaurants, and all toppings
830 belonging to those pizzas. This will result in a total of 3 database queries -
831 one for the restaurants, one for the pizzas, and one for the toppings.
833 >>> Restaurant.objects.prefetch_related('best_pizza__toppings')
835 This will fetch the best pizza and all the toppings for the best pizza for each
836 restaurant. This will be done in 3 database queries - one for the restaurants,
837 one for the 'best pizzas', and one for one for the toppings.
839 Of course, the ``best_pizza`` relationship could also be fetched using
840 ``select_related`` to reduce the query count to 2:
842 >>> Restaurant.objects.select_related('best_pizza').prefetch_related('best_pizza__toppings')
844 Since the prefetch is executed after the main query (which includes the joins
845 needed by ``select_related``), it is able to detect that the ``best_pizza``
846 objects have already been fetched, and it will skip fetching them again.
848 Chaining ``prefetch_related`` calls will accumulate the lookups that are
849 prefetched. To clear any ``prefetch_related`` behavior, pass `None` as a
852 >>> non_prefetched = qs.prefetch_related(None)
854 One difference to note when using ``prefetch_related`` is that objects created
855 by a query can be shared between the different objects that they are related to
856 i.e. a single Python model instance can appear at more than one point in the
857 tree of objects that are returned. This will normally happen with foreign key
858 relationships. Typically this behavior will not be a problem, and will in fact
859 save both memory and CPU time.
861 While ``prefetch_related`` supports prefetching ``GenericForeignKey``
862 relationships, the number of queries will depend on the data. Since a
863 ``GenericForeignKey`` can reference data in multiple tables, one query per table
864 referenced is needed, rather than one query for all the items. There could be
865 additional queries on the ``ContentType`` table if the relevant rows have not
866 already been fetched.
868 ``prefetch_related`` in most cases will be implemented using a SQL query that
869 uses the 'IN' operator. This means that for a large QuerySet a large 'IN' clause
870 could be generated, which, depending on the database, might have performance
871 problems of its own when it comes to parsing or executing the SQL query. Always
872 profile for your use case!
874 Note that if you use ``iterator()`` to run the query, ``prefetch_related()``
875 calls will be ignored since these two optimizations do not make sense together.
880 .. method:: extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)
882 Sometimes, the Django query syntax by itself can't easily express a complex
883 ``WHERE`` clause. For these edge cases, Django provides the ``extra()``
884 ``QuerySet`` modifier — a hook for injecting specific clauses into the SQL
885 generated by a ``QuerySet``.
887 By definition, these extra lookups may not be portable to different database
888 engines (because you're explicitly writing SQL code) and violate the DRY
889 principle, so you should avoid them if possible.
891 Specify one or more of ``params``, ``select``, ``where`` or ``tables``. None
892 of the arguments is required, but you should use at least one of them.
896 The ``select`` argument lets you put extra fields in the ``SELECT``
897 clause. It should be a dictionary mapping attribute names to SQL
898 clauses to use to calculate that attribute.
902 Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
904 As a result, each ``Entry`` object will have an extra attribute,
905 ``is_recent``, a boolean representing whether the entry's ``pub_date``
906 is greater than Jan. 1, 2006.
908 Django inserts the given SQL snippet directly into the ``SELECT``
909 statement, so the resulting SQL of the above example would be something
912 SELECT blog_entry.*, (pub_date > '2006-01-01') AS is_recent
916 The next example is more advanced; it does a subquery to give each
917 resulting ``Blog`` object an ``entry_count`` attribute, an integer count
918 of associated ``Entry`` objects::
922 'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
926 In this particular case, we're exploiting the fact that the query will
927 already contain the ``blog_blog`` table in its ``FROM`` clause.
929 The resulting SQL of the above example would be::
931 SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id) AS entry_count
934 Note that the parentheses required by most database engines around
935 subqueries are not required in Django's ``select`` clauses. Also note
936 that some database backends, such as some MySQL versions, don't support
939 In some rare cases, you might wish to pass parameters to the SQL
940 fragments in ``extra(select=...)``. For this purpose, use the
941 ``select_params`` parameter. Since ``select_params`` is a sequence and
942 the ``select`` attribute is a dictionary, some care is required so that
943 the parameters are matched up correctly with the extra select pieces.
944 In this situation, you should use a
945 :class:`django.utils.datastructures.SortedDict` for the ``select``
946 value, not just a normal Python dictionary.
948 This will work, for example::
951 select=SortedDict([('a', '%s'), ('b', '%s')]),
952 select_params=('one', 'two'))
954 The only thing to be careful about when using select parameters in
955 ``extra()`` is to avoid using the substring ``"%%s"`` (that's *two*
956 percent characters before the ``s``) in the select strings. Django's
957 tracking of parameters looks for ``%s`` and an escaped ``%`` character
958 like this isn't detected. That will lead to incorrect results.
960 * ``where`` / ``tables``
962 You can define explicit SQL ``WHERE`` clauses — perhaps to perform
963 non-explicit joins — by using ``where``. You can manually add tables to
964 the SQL ``FROM`` clause by using ``tables``.
966 ``where`` and ``tables`` both take a list of strings. All ``where``
967 parameters are "AND"ed to any other search criteria.
971 Entry.objects.extra(where=['id IN (3, 4, 5, 20)'])
973 ...translates (roughly) into the following SQL::
975 SELECT * FROM blog_entry WHERE id IN (3, 4, 5, 20);
977 Be careful when using the ``tables`` parameter if you're specifying
978 tables that are already used in the query. When you add extra tables
979 via the ``tables`` parameter, Django assumes you want that table
980 included an extra time, if it is already included. That creates a
981 problem, since the table name will then be given an alias. If a table
982 appears multiple times in an SQL statement, the second and subsequent
983 occurrences must use aliases so the database can tell them apart. If
984 you're referring to the extra table you added in the extra ``where``
985 parameter this is going to cause errors.
987 Normally you'll only be adding extra tables that don't already appear
988 in the query. However, if the case outlined above does occur, there are
989 a few solutions. First, see if you can get by without including the
990 extra table and use the one already in the query. If that isn't
991 possible, put your ``extra()`` call at the front of the queryset
992 construction so that your table is the first use of that table.
993 Finally, if all else fails, look at the query produced and rewrite your
994 ``where`` addition to use the alias given to your extra table. The
995 alias will be the same each time you construct the queryset in the same
996 way, so you can rely upon the alias name to not change.
1000 If you need to order the resulting queryset using some of the new
1001 fields or tables you have included via ``extra()`` use the ``order_by``
1002 parameter to ``extra()`` and pass in a sequence of strings. These
1003 strings should either be model fields (as in the normal
1004 :meth:`order_by()` method on querysets), of the form
1005 ``table_name.column_name`` or an alias for a column that you specified
1006 in the ``select`` parameter to ``extra()``.
1010 q = Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
1011 q = q.extra(order_by = ['-is_recent'])
1013 This would sort all the items for which ``is_recent`` is true to the
1014 front of the result set (``True`` sorts before ``False`` in a
1015 descending ordering).
1017 This shows, by the way, that you can make multiple calls to ``extra()``
1018 and it will behave as you expect (adding new constraints each time).
1022 The ``where`` parameter described above may use standard Python
1023 database string placeholders — ``'%s'`` to indicate parameters the
1024 database engine should automatically quote. The ``params`` argument is
1025 a list of any extra parameters to be substituted.
1029 Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1031 Always use ``params`` instead of embedding values directly into
1032 ``where`` because ``params`` will ensure values are quoted correctly
1033 according to your particular backend. For example, quotes will be
1038 Entry.objects.extra(where=["headline='Lennon'"])
1042 Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1047 .. method:: defer(*fields)
1049 In some complex data-modeling situations, your models might contain a lot of
1050 fields, some of which could contain a lot of data (for example, text fields),
1051 or require expensive processing to convert them to Python objects. If you are
1052 using the results of a queryset in some situation where you know you don't know
1053 if you need those particular fields when you initially fetch the data, you can
1054 tell Django not to retrieve them from the database.
1056 This is done by passing the names of the fields to not load to ``defer()``::
1058 Entry.objects.defer("headline", "body")
1060 A queryset that has deferred fields will still return model instances. Each
1061 deferred field will be retrieved from the database if you access that field
1062 (one at a time, not all the deferred fields at once).
1064 You can make multiple calls to ``defer()``. Each call adds new fields to the
1067 # Defers both the body and headline fields.
1068 Entry.objects.defer("body").filter(rating=5).defer("headline")
1070 The order in which fields are added to the deferred set does not matter.
1071 Calling ``defer()`` with a field name that has already been deferred is
1072 harmless (the field will still be deferred).
1074 You can defer loading of fields in related models (if the related models are
1075 loading via :meth:`select_related()`) by using the standard double-underscore
1076 notation to separate related fields::
1078 Blog.objects.select_related().defer("entry__headline", "entry__body")
1080 If you want to clear the set of deferred fields, pass ``None`` as a parameter
1083 # Load all fields immediately.
1084 my_queryset.defer(None)
1086 Some fields in a model won't be deferred, even if you ask for them. You can
1087 never defer the loading of the primary key. If you are using
1088 :meth:`select_related()` to retrieve related models, you shouldn't defer the
1089 loading of the field that connects from the primary model to the related one
1090 (at the moment, that doesn't raise an error, but it will eventually).
1094 The ``defer()`` method (and its cousin, :meth:`only()`, below) are only for
1095 advanced use-cases. They provide an optimization for when you have analyzed
1096 your queries closely and understand *exactly* what information you need and
1097 have measured that the difference between returning the fields you need and
1098 the full set of fields for the model will be significant.
1100 Even if you think you are in the advanced use-case situation, **only use
1101 defer() when you cannot, at queryset load time, determine if you will need
1102 the extra fields or not**. If you are frequently loading and using a
1103 particular subset of your data, the best choice you can make is to
1104 normalize your models and put the non-loaded data into a separate model
1105 (and database table). If the columns *must* stay in the one table for some
1106 reason, create a model with ``Meta.managed = False`` (see the
1107 :attr:`managed attribute <django.db.models.Options.managed>` documentation)
1108 containing just the fields you normally need to load and use that where you
1109 might otherwise call ``defer()``. This makes your code more explicit to the
1110 reader, is slightly faster and consumes a little less memory in the Python
1117 .. method:: only(*fields)
1119 The ``only()`` method is more or less the opposite of :meth:`defer()`. You call
1120 it with the fields that should *not* be deferred when retrieving a model. If
1121 you have a model where almost all the fields need to be deferred, using
1122 ``only()`` to specify the complementary set of fields can result in simpler
1125 Suppose you have a model with fields ``name``, ``age`` and ``biography``. The
1126 following two querysets are the same, in terms of deferred fields::
1128 Person.objects.defer("age", "biography")
1129 Person.objects.only("name")
1131 Whenever you call ``only()`` it *replaces* the set of fields to load
1132 immediately. The method's name is mnemonic: **only** those fields are loaded
1133 immediately; the remainder are deferred. Thus, successive calls to ``only()``
1134 result in only the final fields being considered::
1136 # This will defer all fields except the headline.
1137 Entry.objects.only("body", "rating").only("headline")
1139 Since ``defer()`` acts incrementally (adding fields to the deferred list), you
1140 can combine calls to ``only()`` and ``defer()`` and things will behave
1143 # Final result is that everything except "headline" is deferred.
1144 Entry.objects.only("headline", "body").defer("body")
1146 # Final result loads headline and body immediately (only() replaces any
1147 # existing set of fields).
1148 Entry.objects.defer("body").only("headline", "body")
1150 All of the cautions in the note for the :meth:`defer` documentation apply to
1151 ``only()`` as well. Use it cautiously and only after exhausting your other
1157 .. method:: using(alias)
1159 .. versionadded:: 1.2
1161 This method is for controlling which database the ``QuerySet`` will be
1162 evaluated against if you are using more than one database. The only argument
1163 this method takes is the alias of a database, as defined in
1164 :setting:`DATABASES`.
1168 # queries the database with the 'default' alias.
1169 >>> Entry.objects.all()
1171 # queries the database with the 'backup' alias
1172 >>> Entry.objects.using('backup')
1177 .. method:: select_for_update(nowait=False)
1179 .. versionadded:: 1.4
1181 Returns a queryset that will lock rows until the end of the transaction,
1182 generating a ``SELECT ... FOR UPDATE`` SQL statement on supported databases.
1186 entries = Entry.objects.select_for_update().filter(author=request.user)
1188 All matched entries will be locked until the end of the transaction block,
1189 meaning that other transactions will be prevented from changing or acquiring
1192 Usually, if another transaction has already acquired a lock on one of the
1193 selected rows, the query will block until the lock is released. If this is
1194 not the behavior you want, call ``select_for_update(nowait=True)``. This will
1195 make the call non-blocking. If a conflicting lock is already acquired by
1196 another transaction, :exc:`~django.db.DatabaseError` will be raised when the
1197 queryset is evaluated.
1199 Note that using ``select_for_update()`` will cause the current transaction to be
1200 considered dirty, if under transaction management. This is to ensure that
1201 Django issues a ``COMMIT`` or ``ROLLBACK``, releasing any locks held by the
1202 ``SELECT FOR UPDATE``.
1204 Currently, the ``postgresql_psycopg2``, ``oracle``, and ``mysql`` database
1205 backends support ``select_for_update()``. However, MySQL has no support for the
1206 ``nowait`` argument. Obviously, users of external third-party backends should
1207 check with their backend's documentation for specifics in those cases.
1209 Passing ``nowait=True`` to ``select_for_update`` using database backends that
1210 do not support ``nowait``, such as MySQL, will cause a
1211 :exc:`~django.db.DatabaseError` to be raised. This is in order to prevent code
1212 unexpectedly blocking.
1214 Using ``select_for_update`` on backends which do not support
1215 ``SELECT ... FOR UPDATE`` (such as SQLite) will have no effect.
1217 Methods that do not return QuerySets
1218 ------------------------------------
1220 The following ``QuerySet`` methods evaluate the ``QuerySet`` and return
1221 something *other than* a ``QuerySet``.
1223 These methods do not use a cache (see :ref:`caching-and-querysets`). Rather,
1224 they query the database each time they're called.
1229 .. method:: get(**kwargs)
1231 Returns the object matching the given lookup parameters, which should be in
1232 the format described in `Field lookups`_.
1234 ``get()`` raises :exc:`~django.core.exceptions.MultipleObjectsReturned` if more
1235 than one object was found. The
1236 :exc:`~django.core.excpetions.MultipleObjectsReturned` exception is an
1237 attribute of the model class.
1239 ``get()`` raises a :exc:`~django.core.exceptions.DoesNotExist` exception if an
1240 object wasn't found for the given parameters. This exception is also an
1241 attribute of the model class. Example::
1243 Entry.objects.get(id='foo') # raises Entry.DoesNotExist
1245 The :exc:`~django.core.exceptions.DoesNotExist` exception inherits from
1246 :exc:`django.core.exceptions.ObjectDoesNotExist`, so you can target multiple
1247 :exc:`~django.core.exceptions.DoesNotExist` exceptions. Example::
1249 from django.core.exceptions import ObjectDoesNotExist
1251 e = Entry.objects.get(id=3)
1252 b = Blog.objects.get(id=1)
1253 except ObjectDoesNotExist:
1254 print "Either the entry or blog doesn't exist."
1259 .. method:: create(**kwargs)
1261 A convenience method for creating an object and saving it all in one step. Thus::
1263 p = Person.objects.create(first_name="Bruce", last_name="Springsteen")
1267 p = Person(first_name="Bruce", last_name="Springsteen")
1268 p.save(force_insert=True)
1272 The :ref:`force_insert <ref-models-force-insert>` parameter is documented
1273 elsewhere, but all it means is that a new object will always be created.
1274 Normally you won't need to worry about this. However, if your model contains a
1275 manual primary key value that you set and if that value already exists in the
1276 database, a call to ``create()`` will fail with an
1277 :exc:`~django.db.IntegrityError` since primary keys must be unique. Be
1278 prepared to handle the exception if you are using manual primary keys.
1283 .. method:: get_or_create(**kwargs)
1285 A convenience method for looking up an object with the given kwargs, creating
1288 Returns a tuple of ``(object, created)``, where ``object`` is the retrieved or
1289 created object and ``created`` is a boolean specifying whether a new object was
1292 This is meant as a shortcut to boilerplatish code and is mostly useful for
1293 data-import scripts. For example::
1296 obj = Person.objects.get(first_name='John', last_name='Lennon')
1297 except Person.DoesNotExist:
1298 obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
1301 This pattern gets quite unwieldy as the number of fields in a model goes up.
1302 The above example can be rewritten using ``get_or_create()`` like so::
1304 obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon',
1305 defaults={'birthday': date(1940, 10, 9)})
1307 Any keyword arguments passed to ``get_or_create()`` — *except* an optional one
1308 called ``defaults`` — will be used in a :meth:`get()` call. If an object is
1309 found, ``get_or_create()`` returns a tuple of that object and ``False``. If an
1310 object is *not* found, ``get_or_create()`` will instantiate and save a new
1311 object, returning a tuple of the new object and ``True``. The new object will
1312 be created roughly according to this algorithm::
1314 defaults = kwargs.pop('defaults', {})
1315 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
1316 params.update(defaults)
1317 obj = self.model(**params)
1320 In English, that means start with any non-``'defaults'`` keyword argument that
1321 doesn't contain a double underscore (which would indicate a non-exact lookup).
1322 Then add the contents of ``defaults``, overriding any keys if necessary, and
1323 use the result as the keyword arguments to the model class. As hinted at
1324 above, this is a simplification of the algorithm that is used, but it contains
1325 all the pertinent details. The internal implementation has some more
1326 error-checking than this and handles some extra edge-conditions; if you're
1327 interested, read the code.
1329 If you have a field named ``defaults`` and want to use it as an exact lookup in
1330 ``get_or_create()``, just use ``'defaults__exact'``, like so::
1332 Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'})
1334 The ``get_or_create()`` method has similar error behavior to :meth:`create()`
1335 when you're using manually specified primary keys. If an object needs to be
1336 created and the key already exists in the database, an
1337 :exc:`~django.db.IntegrityError` will be raised.
1339 Finally, a word on using ``get_or_create()`` in Django views. As mentioned
1340 earlier, ``get_or_create()`` is mostly useful in scripts that need to parse
1341 data and create new records if existing ones aren't available. But if you need
1342 to use ``get_or_create()`` in a view, please make sure to use it only in
1343 ``POST`` requests unless you have a good reason not to. ``GET`` requests
1344 shouldn't have any effect on data; use ``POST`` whenever a request to a page
1345 has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec.
1347 .. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1
1352 .. method:: bulk_create(objs)
1354 .. versionadded:: 1.4
1356 This method inserts the provided list of objects into the database in an
1357 efficient manner (generally only 1 query, no matter how many objects there
1360 >>> Entry.objects.bulk_create([
1361 ... Entry(headline="Django 1.0 Released"),
1362 ... Entry(headline="Django 1.1 Announced"),
1363 ... Entry(headline="Breaking: Django is awesome")
1366 This has a number of caveats though:
1368 * The model's ``save()`` method will not be called, and the ``pre_save`` and
1369 ``post_save`` signals will not be sent.
1370 * It does not work with child models in a multi-table inheritance scenario.
1371 * If the model's primary key is an :class:`~django.db.models.AutoField` it
1372 does not retrieve and set the primary key attribute, as ``save()`` does.
1374 .. admonition:: Limits of SQLite
1376 SQLite sets a limit on the number of parameters per SQL statement. The
1377 maximum is defined by the SQLITE_MAX_VARIABLE_NUMBER_ compilation option,
1378 which defaults to 999. For instance, if your model has 8 fields (including
1379 the primary key), you cannot create more than 999 // 8 = 124 instances at
1380 a time. If you exceed this limit, you'll get an exception::
1382 django.db.utils.DatabaseError: too many SQL variables
1384 If your application's performance requirements exceed SQLite's limits, you
1385 should switch to another database engine, such as PostgreSQL.
1387 .. _SQLITE_MAX_VARIABLE_NUMBER: http://sqlite.org/limits.html#max_variable_number
1394 Returns an integer representing the number of objects in the database matching
1395 the ``QuerySet``. The ``count()`` method never raises exceptions.
1399 # Returns the total number of entries in the database.
1400 Entry.objects.count()
1402 # Returns the number of entries whose headline contains 'Lennon'
1403 Entry.objects.filter(headline__contains='Lennon').count()
1405 A ``count()`` call performs a ``SELECT COUNT(*)`` behind the scenes, so you
1406 should always use ``count()`` rather than loading all of the record into Python
1407 objects and calling ``len()`` on the result (unless you need to load the
1408 objects into memory anyway, in which case ``len()`` will be faster).
1410 Depending on which database you're using (e.g. PostgreSQL vs. MySQL),
1411 ``count()`` may return a long integer instead of a normal Python integer. This
1412 is an underlying implementation quirk that shouldn't pose any real-world
1418 .. method:: in_bulk(id_list)
1420 Takes a list of primary-key values and returns a dictionary mapping each
1421 primary-key value to an instance of the object with the given ID.
1425 >>> Blog.objects.in_bulk([1])
1426 {1: <Blog: Beatles Blog>}
1427 >>> Blog.objects.in_bulk([1, 2])
1428 {1: <Blog: Beatles Blog>, 2: <Blog: Cheddar Talk>}
1429 >>> Blog.objects.in_bulk([])
1432 If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary.
1437 .. method:: iterator()
1439 Evaluates the ``QuerySet`` (by performing the query) and returns an iterator
1440 (see :pep:`234`) over the results. A ``QuerySet`` typically caches its results
1441 internally so that repeated evaluations do not result in additional queries. In
1442 contrast, ``iterator()`` will read results directly, without doing any caching
1443 at the ``QuerySet`` level (internally, the default iterator calls ``iterator()``
1444 and caches the return value). For a ``QuerySet`` which returns a large number of
1445 objects that you only need to access once, this can results in better
1446 performance and a significant reduction in memory.
1448 Note that using ``iterator()`` on a ``QuerySet`` which has already been
1449 evaluated will force it to evaluate again, repeating the query.
1451 Also, use of ``iterator()`` causes previous ``prefetch_related()`` calls to be
1452 ignored since these two optimizations do not make sense together.
1457 .. method:: latest(field_name=None)
1459 Returns the latest object in the table, by date, using the ``field_name``
1460 provided as the date field.
1462 This example returns the latest ``Entry`` in the table, according to the
1463 ``pub_date`` field::
1465 Entry.objects.latest('pub_date')
1467 If your model's :ref:`Meta <meta-options>` specifies
1468 :attr:`~django.db.models.Options.get_latest_by`, you can leave off the
1469 ``field_name`` argument to ``latest()``. Django will use the field specified
1470 in :attr:`~django.db.models.Options.get_latest_by` by default.
1472 Like :meth:`get()`, ``latest()`` raises
1473 :exc:`~django.core.exceptions.DoesNotExist` if there is no object with the given
1476 Note ``latest()`` exists purely for convenience and readability.
1481 .. method:: aggregate(*args, **kwargs)
1483 Returns a dictionary of aggregate values (averages, sums, etc) calculated over
1484 the ``QuerySet``. Each argument to ``aggregate()`` specifies a value that will
1485 be included in the dictionary that is returned.
1487 The aggregation functions that are provided by Django are described in
1488 `Aggregation Functions`_ below.
1490 Aggregates specified using keyword arguments will use the keyword as the name
1491 for the annotation. Anonymous arguments will have a name generated for them
1492 based upon the name of the aggregate function and the model field that is being
1495 For example, when you are working with blog entries, you may want to know the
1496 number of authors that have contributed blog entries::
1498 >>> q = Blog.objects.aggregate(Count('entry'))
1499 {'entry__count': 16}
1501 By using a keyword argument to specify the aggregate function, you can
1502 control the name of the aggregation value that is returned::
1504 >>> q = Blog.objects.aggregate(number_of_entries=Count('entry'))
1505 {'number_of_entries': 16}
1507 For an in-depth discussion of aggregation, see :doc:`the topic guide on
1508 Aggregation </topics/db/aggregation>`.
1513 .. method:: exists()
1515 .. versionadded:: 1.2
1517 Returns ``True`` if the :class:`.QuerySet` contains any results, and ``False``
1518 if not. This tries to perform the query in the simplest and fastest way
1519 possible, but it *does* execute nearly the same query. This means that calling
1520 :meth:`.QuerySet.exists` is faster than ``bool(some_query_set)``, but not by
1521 a large degree. If ``some_query_set`` has not yet been evaluated, but you know
1522 that it will be at some point, then using ``some_query_set.exists()`` will do
1523 more overall work (one query for the existence check plus an extra one to later
1524 retrieve the results) than simply using ``bool(some_query_set)``, which
1525 retrieves the results and then checks if any were returned.
1530 .. method:: update(**kwargs)
1532 Performs an SQL update query for the specified fields, and returns
1533 the number of rows matched (which may not be equal to the number of rows
1534 updated if some rows already have the new value).
1536 For example, to turn comments off for all blog entries published in 2010,
1539 >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)
1541 (This assumes your ``Entry`` model has fields ``pub_date`` and ``comments_on``.)
1543 You can update multiple fields — there's no limit on how many. For example,
1544 here we update the ``comments_on`` and ``headline`` fields::
1546 >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False, headline='This is old')
1548 The ``update()`` method is applied instantly, and the only restriction on the
1549 :class:`.QuerySet` that is updated is that it can only update columns in the
1550 model's main table, not on related models. You can't do this, for example::
1552 >>> Entry.objects.update(blog__name='foo') # Won't work!
1554 Filtering based on related fields is still possible, though::
1556 >>> Entry.objects.filter(blog__id=1).update(comments_on=True)
1558 You cannot call ``update()`` on a :class:`.QuerySet` that has had a slice taken
1559 or can otherwise no longer be filtered.
1561 The ``update()`` method returns the number of affected rows::
1563 >>> Entry.objects.filter(id=64).update(comments_on=True)
1566 >>> Entry.objects.filter(slug='nonexistent-slug').update(comments_on=True)
1569 >>> Entry.objects.filter(pub_date__year=2010).update(comments_on=False)
1572 If you're just updating a record and don't need to do anything with the model
1573 object, the most efficient approach is to call ``update()``, rather than
1574 loading the model object into memory. For example, instead of doing this::
1576 e = Entry.objects.get(id=10)
1577 e.comments_on = False
1582 Entry.objects.filter(id=10).update(comments_on=False)
1584 Using ``update()`` also prevents a race condition wherein something might
1585 change in your database in the short period of time between loading the object
1586 and calling ``save()``.
1588 Finally, realize that ``update()`` does an update at the SQL level and, thus,
1589 does not call any ``save()`` methods on your models, nor does it emit the
1590 :attr:`~django.db.models.signals.pre_save` or
1591 :attr:`~django.db.models.signals.post_save` signals (which are a consequence of
1592 calling :meth:`Model.save() <~django.db.models.Model.save()>`). If you want to
1593 update a bunch of records for a model that has a custom
1594 :meth:`~django.db.models.Model.save()`` method, loop over them and call
1595 :meth:`~django.db.models.Model.save()`, like this::
1597 for e in Entry.objects.filter(pub_date__year=2010):
1598 e.comments_on = False
1604 .. method:: delete()
1606 Performs an SQL delete query on all rows in the :class:`.QuerySet`. The
1607 ``delete()`` is applied instantly. You cannot call ``delete()`` on a
1608 :class:`.QuerySet` that has had a slice taken or can otherwise no longer be
1611 For example, to delete all the entries in a particular blog::
1613 >>> b = Blog.objects.get(pk=1)
1615 # Delete all the entries belonging to this Blog.
1616 >>> Entry.objects.filter(blog=b).delete()
1618 By default, Django's :class:`~django.db.models.ForeignKey` emulates the SQL
1619 constraint ``ON DELETE CASCADE`` — in other words, any objects with foreign
1620 keys pointing at the objects to be deleted will be deleted along with them.
1623 blogs = Blog.objects.all()
1624 # This will delete all Blogs and all of their Entry objects.
1627 .. versionadded:: 1.3
1628 This cascade behavior is customizable via the
1629 :attr:`~django.db.models.ForeignKey.on_delete` argument to the
1630 :class:`~django.db.models.ForeignKey`.
1632 The ``delete()`` method does a bulk delete and does not call any ``delete()``
1633 methods on your models. It does, however, emit the
1634 :data:`~django.db.models.signals.pre_delete` and
1635 :data:`~django.db.models.signals.post_delete` signals for all deleted objects
1636 (including cascaded deletions).
1643 Field lookups are how you specify the meat of an SQL ``WHERE`` clause. They're
1644 specified as keyword arguments to the ``QuerySet`` methods :meth:`filter()`,
1645 :meth:`exclude()` and :meth:`get()`.
1647 For an introduction, see :ref:`models and database queries documentation
1648 <field-lookups-intro>`.
1650 .. fieldlookup:: exact
1655 Exact match. If the value provided for comparison is ``None``, it will be
1656 interpreted as an SQL ``NULL`` (see :lookup:`isnull` for more details).
1660 Entry.objects.get(id__exact=14)
1661 Entry.objects.get(id__exact=None)
1665 SELECT ... WHERE id = 14;
1666 SELECT ... WHERE id IS NULL;
1668 .. admonition:: MySQL comparisons
1670 In MySQL, a database table's "collation" setting determines whether
1671 ``exact`` comparisons are case-sensitive. This is a database setting, *not*
1672 a Django setting. It's possible to configure your MySQL tables to use
1673 case-sensitive comparisons, but some trade-offs are involved. For more
1674 information about this, see the :ref:`collation section <mysql-collation>`
1675 in the :doc:`databases </ref/databases>` documentation.
1677 .. fieldlookup:: iexact
1682 Case-insensitive exact match.
1686 Blog.objects.get(name__iexact='beatles blog')
1690 SELECT ... WHERE name ILIKE 'beatles blog';
1692 Note this will match ``'Beatles Blog'``, ``'beatles blog'``, ``'BeAtLes
1695 .. admonition:: SQLite users
1697 When using the SQLite backend and Unicode (non-ASCII) strings, bear in
1698 mind the :ref:`database note <sqlite-string-matching>` about string
1699 comparisons. SQLite does not do case-insensitive matching for Unicode
1702 .. fieldlookup:: contains
1707 Case-sensitive containment test.
1711 Entry.objects.get(headline__contains='Lennon')
1715 SELECT ... WHERE headline LIKE '%Lennon%';
1717 Note this will match the headline ``'Lennon honored today'`` but not ``'lennon
1720 .. admonition:: SQLite users
1722 SQLite doesn't support case-sensitive ``LIKE`` statements; ``contains``
1723 acts like ``icontains`` for SQLite. See the :ref:`database note
1724 <sqlite-string-matching>` for more information.
1727 .. fieldlookup:: icontains
1732 Case-insensitive containment test.
1736 Entry.objects.get(headline__icontains='Lennon')
1740 SELECT ... WHERE headline ILIKE '%Lennon%';
1742 .. admonition:: SQLite users
1744 When using the SQLite backend and Unicode (non-ASCII) strings, bear in
1745 mind the :ref:`database note <sqlite-string-matching>` about string
1757 Entry.objects.filter(id__in=[1, 3, 4])
1761 SELECT ... WHERE id IN (1, 3, 4);
1763 You can also use a queryset to dynamically evaluate the list of values
1764 instead of providing a list of literal values::
1766 inner_qs = Blog.objects.filter(name__contains='Cheddar')
1767 entries = Entry.objects.filter(blog__in=inner_qs)
1769 This queryset will be evaluated as subselect statement::
1771 SELECT ... WHERE blog.id IN (SELECT id FROM ... WHERE NAME LIKE '%Cheddar%')
1773 The above code fragment could also be written as follows::
1775 inner_q = Blog.objects.filter(name__contains='Cheddar').values('pk').query
1776 entries = Entry.objects.filter(blog__in=inner_q)
1780 This ``query`` attribute should be considered an opaque internal attribute.
1781 It's fine to use it like above, but its API may change between Django
1784 This second form is a bit less readable and unnatural to write, since it
1785 accesses the internal ``query`` attribute and requires a ``ValuesQuerySet``.
1786 If your code doesn't require compatibility with Django 1.0, use the first
1787 form, passing in a queryset directly.
1789 If you pass in a ``ValuesQuerySet`` or ``ValuesListQuerySet`` (the result of
1790 calling ``values()`` or ``values_list()`` on a queryset) as the value to an
1791 ``__in`` lookup, you need to ensure you are only extracting one field in the
1792 result. For example, this will work (filtering on the blog names)::
1794 inner_qs = Blog.objects.filter(name__contains='Ch').values('name')
1795 entries = Entry.objects.filter(blog__name__in=inner_qs)
1797 This example will raise an exception, since the inner query is trying to
1798 extract two field values, where only one is expected::
1800 # Bad code! Will raise a TypeError.
1801 inner_qs = Blog.objects.filter(name__contains='Ch').values('name', 'id')
1802 entries = Entry.objects.filter(blog__name__in=inner_qs)
1804 .. admonition:: Performance considerations
1806 Be cautious about using nested queries and understand your database
1807 server's performance characteristics (if in doubt, benchmark!). Some
1808 database backends, most notably MySQL, don't optimize nested queries very
1809 well. It is more efficient, in those cases, to extract a list of values
1810 and then pass that into the second query. That is, execute two queries
1813 values = Blog.objects.filter(
1814 name__contains='Cheddar').values_list('pk', flat=True)
1815 entries = Entry.objects.filter(blog__in=list(values))
1817 Note the ``list()`` call around the Blog ``QuerySet`` to force execution of
1818 the first query. Without it, a nested query would be executed, because
1819 :ref:`querysets-are-lazy`.
1830 Entry.objects.filter(id__gt=4)
1834 SELECT ... WHERE id > 4;
1836 .. fieldlookup:: gte
1841 Greater than or equal to.
1850 .. fieldlookup:: lte
1855 Less than or equal to.
1857 .. fieldlookup:: startswith
1862 Case-sensitive starts-with.
1866 Entry.objects.filter(headline__startswith='Will')
1870 SELECT ... WHERE headline LIKE 'Will%';
1872 SQLite doesn't support case-sensitive ``LIKE`` statements; ``startswith`` acts
1873 like ``istartswith`` for SQLite.
1875 .. fieldlookup:: istartswith
1880 Case-insensitive starts-with.
1884 Entry.objects.filter(headline__istartswith='will')
1888 SELECT ... WHERE headline ILIKE 'Will%';
1890 .. admonition:: SQLite users
1892 When using the SQLite backend and Unicode (non-ASCII) strings, bear in
1893 mind the :ref:`database note <sqlite-string-matching>` about string
1896 .. fieldlookup:: endswith
1901 Case-sensitive ends-with.
1905 Entry.objects.filter(headline__endswith='cats')
1909 SELECT ... WHERE headline LIKE '%cats';
1911 .. admonition:: SQLite users
1913 SQLite doesn't support case-sensitive ``LIKE`` statements; ``endswith``
1914 acts like ``iendswith`` for SQLite. Refer to the :ref:`database note
1915 <sqlite-string-matching>` documentation for more.
1917 .. fieldlookup:: iendswith
1922 Case-insensitive ends-with.
1926 Entry.objects.filter(headline__iendswith='will')
1930 SELECT ... WHERE headline ILIKE '%will'
1932 .. admonition:: SQLite users
1934 When using the SQLite backend and Unicode (non-ASCII) strings, bear in
1935 mind the :ref:`database note <sqlite-string-matching>` about string
1938 .. fieldlookup:: range
1943 Range test (inclusive).
1947 start_date = datetime.date(2005, 1, 1)
1948 end_date = datetime.date(2005, 3, 31)
1949 Entry.objects.filter(pub_date__range=(start_date, end_date))
1953 SELECT ... WHERE pub_date BETWEEN '2005-01-01' and '2005-03-31';
1955 You can use ``range`` anywhere you can use ``BETWEEN`` in SQL — for dates,
1956 numbers and even characters.
1958 .. fieldlookup:: year
1963 For date/datetime fields, exact year match. Takes a four-digit year.
1967 Entry.objects.filter(pub_date__year=2005)
1971 SELECT ... WHERE pub_date BETWEEN '2005-01-01' AND '2005-12-31 23:59:59.999999';
1973 (The exact SQL syntax varies for each database engine.)
1975 .. fieldlookup:: month
1980 For date and datetime fields, an exact month match. Takes an integer 1
1981 (January) through 12 (December).
1985 Entry.objects.filter(pub_date__month=12)
1989 SELECT ... WHERE EXTRACT('month' FROM pub_date) = '12';
1991 (The exact SQL syntax varies for each database engine.)
1993 .. fieldlookup:: day
1998 For date and datetime fields, an exact day match.
2002 Entry.objects.filter(pub_date__day=3)
2006 SELECT ... WHERE EXTRACT('day' FROM pub_date) = '3';
2008 (The exact SQL syntax varies for each database engine.)
2010 Note this will match any record with a pub_date on the third day of the month,
2011 such as January 3, July 3, etc.
2013 .. fieldlookup:: week_day
2018 For date and datetime fields, a 'day of the week' match.
2020 Takes an integer value representing the day of week from 1 (Sunday) to 7
2025 Entry.objects.filter(pub_date__week_day=2)
2027 (No equivalent SQL code fragment is included for this lookup because
2028 implementation of the relevant query varies among different database engines.)
2030 Note this will match any record with a ``pub_date`` that falls on a Monday (day
2031 2 of the week), regardless of the month or year in which it occurs. Week days
2032 are indexed with day 1 being Sunday and day 7 being Saturday.
2036 When :doc:`time zone support </topics/i18n/timezones>` is enabled, Django
2037 uses UTC in the database connection, which means the ``year``, ``month``,
2038 ``day`` and ``week_day`` lookups are performed in UTC. This is a known
2039 limitation of the current implementation.
2041 .. fieldlookup:: isnull
2046 Takes either ``True`` or ``False``, which correspond to SQL queries of
2047 ``IS NULL`` and ``IS NOT NULL``, respectively.
2051 Entry.objects.filter(pub_date__isnull=True)
2055 SELECT ... WHERE pub_date IS NULL;
2057 .. fieldlookup:: search
2062 A boolean full-text search, taking advantage of full-text indexing. This is
2063 like :lookup:`contains` but is significantly faster due to full-text indexing.
2067 Entry.objects.filter(headline__search="+Django -jazz Python")
2071 SELECT ... WHERE MATCH(tablename, headline) AGAINST (+Django -jazz Python IN BOOLEAN MODE);
2073 Note this is only available in MySQL and requires direct manipulation of the
2074 database to add the full-text index. By default Django uses BOOLEAN MODE for
2075 full text searches. See the `MySQL documentation`_ for additional details.
2077 .. _MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
2080 .. fieldlookup:: regex
2085 Case-sensitive regular expression match.
2087 The regular expression syntax is that of the database backend in use.
2088 In the case of SQLite, which has no built in regular expression support,
2089 this feature is provided by a (Python) user-defined REGEXP function, and
2090 the regular expression syntax is therefore that of Python's ``re`` module.
2094 Entry.objects.get(title__regex=r'^(An?|The) +')
2098 SELECT ... WHERE title REGEXP BINARY '^(An?|The) +'; -- MySQL
2100 SELECT ... WHERE REGEXP_LIKE(title, '^(an?|the) +', 'c'); -- Oracle
2102 SELECT ... WHERE title ~ '^(An?|The) +'; -- PostgreSQL
2104 SELECT ... WHERE title REGEXP '^(An?|The) +'; -- SQLite
2106 Using raw strings (e.g., ``r'foo'`` instead of ``'foo'``) for passing in the
2107 regular expression syntax is recommended.
2109 .. fieldlookup:: iregex
2114 Case-insensitive regular expression match.
2118 Entry.objects.get(title__iregex=r'^(an?|the) +')
2122 SELECT ... WHERE title REGEXP '^(an?|the) +'; -- MySQL
2124 SELECT ... WHERE REGEXP_LIKE(title, '^(an?|the) +', 'i'); -- Oracle
2126 SELECT ... WHERE title ~* '^(an?|the) +'; -- PostgreSQL
2128 SELECT ... WHERE title REGEXP '(?i)^(an?|the) +'; -- SQLite
2130 .. _aggregation-functions:
2132 Aggregation functions
2133 ---------------------
2135 .. currentmodule:: django.db.models
2137 Django provides the following aggregation functions in the
2138 ``django.db.models`` module. For details on how to use these
2139 aggregate functions, see
2140 :doc:`the topic guide on aggregation </topics/db/aggregation>`.
2145 .. class:: Avg(field)
2147 Returns the mean value of the given field, which must be numeric.
2149 * Default alias: ``<field>__avg``
2150 * Return type: ``float``
2155 .. class:: Count(field, distinct=False)
2157 Returns the number of objects that are related through the provided field.
2159 * Default alias: ``<field>__count``
2160 * Return type: ``int``
2162 Has one optional argument:
2164 .. attribute:: distinct
2166 If ``distinct=True``, the count will only include unique instances.
2167 This is the SQL equivalent of ``COUNT(DISTINCT <field>)``. The default
2173 .. class:: Max(field)
2175 Returns the maximum value of the given field.
2177 * Default alias: ``<field>__max``
2178 * Return type: same as input field
2183 .. class:: Min(field)
2185 Returns the minimum value of the given field.
2187 * Default alias: ``<field>__min``
2188 * Return type: same as input field
2193 .. class:: StdDev(field, sample=False)
2195 Returns the standard deviation of the data in the provided field.
2197 * Default alias: ``<field>__stddev``
2198 * Return type: ``float``
2200 Has one optional argument:
2202 .. attribute:: sample
2204 By default, ``StdDev`` returns the population standard deviation. However,
2205 if ``sample=True``, the return value will be the sample standard deviation.
2207 .. admonition:: SQLite
2209 SQLite doesn't provide ``StdDev`` out of the box. An implementation
2210 is available as an extension module for SQLite. Consult the `SQlite
2211 documentation`_ for instructions on obtaining and installing this
2217 .. class:: Sum(field)
2219 Computes the sum of all values of the given field.
2221 * Default alias: ``<field>__sum``
2222 * Return type: same as input field
2227 .. class:: Variance(field, sample=False)
2229 Returns the variance of the data in the provided field.
2231 * Default alias: ``<field>__variance``
2232 * Return type: ``float``
2234 Has one optional argument:
2236 .. attribute:: sample
2238 By default, ``Variance`` returns the population variance. However,
2239 if ``sample=True``, the return value will be the sample variance.
2241 .. admonition:: SQLite
2243 SQLite doesn't provide ``Variance`` out of the box. An implementation
2244 is available as an extension module for SQLite. Consult the `SQlite
2245 documentation`_ for instructions on obtaining and installing this
2248 .. _SQLite documentation: http://www.sqlite.org/contrib