Django

Code

root/django/trunk/docs/db-api.txt

Revision 7499, 83.1 kB (checked in by mtredinnick, 3 weeks ago)

Added the ability to pickle and unpickle QuerySets? and Query classes.

  • Property svn:eol-style set to native
Line 
1 ======================
2 Database API reference
3 ======================
4
5 Once you've created your `data models`_, Django automatically gives you a
6 database-abstraction API that lets you create, retrieve, update and delete
7 objects. This document explains that API.
8
9 .. _`data models`: ../model-api/
10
11 Throughout this reference, we'll refer to the following models, which comprise
12 a weblog application::
13
14     class Blog(models.Model):
15         name = models.CharField(max_length=100)
16         tagline = models.TextField()
17
18         def __unicode__(self):
19             return self.name
20
21     class Author(models.Model):
22         name = models.CharField(max_length=50)
23         email = models.EmailField()
24
25         def __unicode__(self):
26             return self.name
27
28     class Entry(models.Model):
29         blog = models.ForeignKey(Blog)
30         headline = models.CharField(max_length=255)
31         body_text = models.TextField()
32         pub_date = models.DateTimeField()
33         authors = models.ManyToManyField(Author)
34
35         def __unicode__(self):
36             return self.headline
37
38 Creating objects
39 ================
40
41 To represent database-table data in Python objects, Django uses an intuitive
42 system: A model class represents a database table, and an instance of that
43 class represents a particular record in the database table.
44
45 To create an object, instantiate it using keyword arguments to the model class,
46 then call ``save()`` to save it to the database.
47
48 You import the model class from wherever it lives on the Python path, as you
49 may expect. (We point this out here because previous Django versions required
50 funky model importing.)
51
52 Assuming models live in a file ``mysite/blog/models.py``, here's an example::
53
54     from mysite.blog.models import Blog
55     b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.')
56     b.save()
57
58 This performs an ``INSERT`` SQL statement behind the scenes. Django doesn't hit
59 the database until you explicitly call ``save()``.
60
61 The ``save()`` method has no return value.
62
63 To create an object and save it all in one step see the `create`__ method.
64
65 __ `create(**kwargs)`_
66
67 Auto-incrementing primary keys
68 ------------------------------
69
70 If a model has an ``AutoField`` -- an auto-incrementing primary key -- then
71 that auto-incremented value will be calculated and saved as an attribute on
72 your object the first time you call ``save()``.
73
74 Example::
75
76     b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.')
77     b2.id     # Returns None, because b doesn't have an ID yet.
78     b2.save()
79     b2.id     # Returns the ID of your new object.
80
81 There's no way to tell what the value of an ID will be before you call
82 ``save()``, because that value is calculated by your database, not by Django.
83
84 (For convenience, each model has an ``AutoField`` named ``id`` by default
85 unless you explicitly specify ``primary_key=True`` on a field. See the
86 `AutoField documentation`_.)
87
88 .. _AutoField documentation: ../model-api/#autofield
89
90 Explicitly specifying auto-primary-key values
91 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
92
93 If a model has an ``AutoField`` but you want to define a new object's ID
94 explicitly when saving, just define it explicitly before saving, rather than
95 relying on the auto-assignment of the ID.
96
97 Example::
98
99     b3 = Blog(id=3, name='Cheddar Talk', tagline='Thoughts on cheese.')
100     b3.id     # Returns 3.
101     b3.save()
102     b3.id     # Returns 3.
103
104 If you assign auto-primary-key values manually, make sure not to use an
105 already-existing primary-key value! If you create a new object with an explicit
106 primary-key value that already exists in the database, Django will assume
107 you're changing the existing record rather than creating a new one.
108
109 Given the above ``'Cheddar Talk'`` blog example, this example would override
110 the previous record in the database::
111
112     b4 = Blog(id=3, name='Not Cheddar', tagline='Anything but cheese.')
113     b4.save()  # Overrides the previous blog with ID=3!
114
115 See `How Django knows to UPDATE vs. INSERT`_, below, for the reason this
116 happens.
117
118 Explicitly specifying auto-primary-key values is mostly useful for bulk-saving
119 objects, when you're confident you won't have primary-key collision.
120
121 What happens when you save?
122 ---------------------------
123
124 When you save an object, Django performs the following steps:
125
126     1. **Emit a ``pre_save`` signal.** This provides a notification that
127        an object is about to be saved. You can register a listener that
128        will be invoked whenever this signal is emitted. (These signals are
129        not yet documented.)
130
131     2. **Pre-process the data.** Each field on the object is asked to
132        perform any automated data modification that the field may need
133        to perform.
134
135        Most fields do *no* pre-processing -- the field data is kept as-is.
136        Pre-processing is only used on fields that have special behavior.
137        For example, if your model has a ``DateField`` with ``auto_now=True``,
138        the pre-save phase will alter the data in the object to ensure that
139        the date field contains the current date stamp. (Our documentation
140        doesn't yet include a list of all the fields with this "special
141        behavior.")
142
143     3. **Prepare the data for the database.** Each field is asked to provide
144        its current value in a data type that can be written to the database.
145
146        Most fields require *no* data preparation. Simple data types, such as
147        integers and strings, are 'ready to write' as a Python object. However,
148        more complex data types often require some modification.
149
150        For example, ``DateFields`` use a Python ``datetime`` object to store
151        data. Databases don't store ``datetime`` objects, so the field value
152        must be converted into an ISO-compliant date string for insertion
153        into the database.
154
155     4. **Insert the data into the database.** The pre-processed, prepared
156        data is then composed into an SQL statement for insertion into the
157        database.
158
159     5. **Emit a ``post_save`` signal.** As with the ``pre_save`` signal, this
160        is used to provide notification that an object has been successfully
161        saved. (These signals are not yet documented.)
162
163 Saving changes to objects
164 =========================
165
166 To save changes to an object that's already in the database, use ``save()``.
167
168 Given a ``Blog`` instance ``b5`` that has already been saved to the database,
169 this example changes its name and updates its record in the database::
170
171     b5.name = 'New name'
172     b5.save()
173
174 This performs an ``UPDATE`` SQL statement behind the scenes. Django doesn't hit
175 the database until you explicitly call ``save()``.
176
177 The ``save()`` method has no return value.
178
179 Saving ForeignKey and ManyToManyField fields
180 --------------------------------------------
181
182 Updating ``ForeignKey`` fields works exactly the same way as saving a normal
183 field; simply assign an object of the right type to the field in question::
184
185     cheese_blog = Blog.objects.get(name="Cheddar Talk")
186     entry.blog = cheese_blog
187     entry.save()
188
189 Updating a ``ManyToManyField`` works a little differently; use the ``add()``
190 method on the field to add a record to the relation::
191
192     joe = Author.objects.create(name="Joe")
193     entry.authors.add(joe)
194
195 Django will complain if you try to assign or add an object of the wrong type.
196
197 How Django knows to UPDATE vs. INSERT
198 -------------------------------------
199
200 You may have noticed Django database objects use the same ``save()`` method
201 for creating and changing objects. Django abstracts the need to use ``INSERT``
202 or ``UPDATE`` SQL statements. Specifically, when you call ``save()``, Django
203 follows this algorithm:
204
205     * If the object's primary key attribute is set to a value that evaluates to
206       ``True`` (i.e., a value other than ``None`` or the empty string), Django
207       executes a ``SELECT`` query to determine whether a record with the given
208       primary key already exists.
209     * If the record with the given primary key does already exist, Django
210       executes an ``UPDATE`` query.
211     * If the object's primary key attribute is *not* set, or if it's set but a
212       record doesn't exist, Django executes an ``INSERT``.
213
214 The one gotcha here is that you should be careful not to specify a primary-key
215 value explicitly when saving new objects, if you cannot guarantee the
216 primary-key value is unused. For more on this nuance, see
217 "Explicitly specifying auto-primary-key values" above.
218
219 Retrieving objects
220 ==================
221
222 To retrieve objects from your database, you construct a ``QuerySet`` via a
223 ``Manager`` on your model class.
224
225 A ``QuerySet`` represents a collection of objects from your database. It can
226 have zero, one or many *filters* -- criteria that narrow down the collection
227 based on given parameters. In SQL terms, a ``QuerySet`` equates to a ``SELECT``
228 statement, and a filter is a limiting clause such as ``WHERE`` or ``LIMIT``.
229
230 You get a ``QuerySet`` by using your model's ``Manager``. Each model has at
231 least one ``Manager``, and it's called ``objects`` by default. Access it
232 directly via the model class, like so::
233
234     Blog.objects  # <django.db.models.manager.Manager object at ...>
235     b = Blog(name='Foo', tagline='Bar')
236     b.objects     # AttributeError: "Manager isn't accessible via Blog instances."
237
238 (``Managers`` are accessible only via model classes, rather than from model
239 instances, to enforce a separation between "table-level" operations and
240 "record-level" operations.)
241
242 The ``Manager`` is the main source of ``QuerySets`` for a model. It acts as a
243 "root" ``QuerySet`` that describes all objects in the model's database table.
244 For example, ``Blog.objects`` is the initial ``QuerySet`` that contains all
245 ``Blog`` objects in the database.
246
247 Retrieving all objects
248 ----------------------
249
250 The simplest way to retrieve objects from a table is to get all of them.
251 To do this, use the ``all()`` method on a ``Manager``.
252
253 Example::
254
255     all_entries = Entry.objects.all()
256
257 The ``all()`` method returns a ``QuerySet`` of all the objects in the database.
258
259 (If ``Entry.objects`` is a ``QuerySet``, why can't we just do ``Entry.objects``?
260 That's because ``Entry.objects``, the root ``QuerySet``, is a special case
261 that cannot be evaluated. The ``all()`` method returns a ``QuerySet`` that
262 *can* be evaluated.)
263
264 Filtering objects
265 -----------------
266
267 The root ``QuerySet`` provided by the ``Manager`` describes all objects in the
268 database table. Usually, though, you'll need to select only a subset of the
269 complete set of objects.
270
271 To create such a subset, you refine the initial ``QuerySet``, adding filter
272 conditions. The two most common ways to refine a ``QuerySet`` are:
273
274 ``filter(**kwargs)``
275     Returns a new ``QuerySet`` containing objects that match the given lookup
276     parameters.
277
278 ``exclude(**kwargs)``
279     Returns a new ``QuerySet`` containing objects that do *not* match the given
280     lookup parameters.
281
282 The lookup parameters (``**kwargs`` in the above function definitions) should
283 be in the format described in `Field lookups`_ below.
284
285 For example, to get a ``QuerySet`` of blog entries from the year 2006, use
286 ``filter()`` like so::
287
288     Entry.objects.filter(pub_date__year=2006)
289
290 (Note we don't have to add an ``all()`` -- ``Entry.objects.all().filter(...)``.
291 That would still work, but you only need ``all()`` when you want all objects
292 from the root ``QuerySet``.)
293
294 Chaining filters
295 ~~~~~~~~~~~~~~~~
296
297 The result of refining a ``QuerySet`` is itself a ``QuerySet``, so it's
298 possible to chain refinements together. For example::
299
300     Entry.objects.filter(
301         headline__startswith='What').exclude(
302             pub_date__gte=datetime.now()).filter(
303                 pub_date__gte=datetime(2005, 1, 1))
304
305 ...takes the initial ``QuerySet`` of all entries in the database, adds a
306 filter, then an exclusion, then another filter. The final result is a
307 ``QuerySet`` containing all entries with a headline that starts with "What",
308 that were published between January 1, 2005, and the current day.
309
310 Filtered QuerySets are unique
311 -----------------------------
312
313 Each time you refine a ``QuerySet``, you get a brand-new ``QuerySet`` that is
314 in no way bound to the previous ``QuerySet``. Each refinement creates a
315 separate and distinct ``QuerySet`` that can be stored, used and reused.
316
317 Example::
318
319     q1 = Entry.objects.filter(headline__startswith="What")
320     q2 = q1.exclude(pub_date__gte=datetime.now())
321     q3 = q1.filter(pub_date__gte=datetime.now())
322
323 These three ``QuerySets`` are separate. The first is a base ``QuerySet``
324 containing all entries that contain a headline starting with "What". The second
325 is a subset of the first, with an additional criteria that excludes records
326 whose ``pub_date`` is greater than now. The third is a subset of the first,
327 with an additional criteria that selects only the records whose ``pub_date`` is
328 greater than now. The initial ``QuerySet`` (``q1``) is unaffected by the
329 refinement process.
330
331 QuerySets are lazy
332 ------------------
333
334 ``QuerySets`` are lazy -- the act of creating a ``QuerySet`` doesn't involve
335 any database activity. You can stack filters together all day long, and Django
336 won't actually run the query until the ``QuerySet`` is *evaluated*.
337
338 When QuerySets are evaluated
339 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
340
341 You can evaluate a ``QuerySet`` in the following ways:
342
343     * **Iteration.** A ``QuerySet`` is iterable, and it executes its database
344       query the first time you iterate over it. For example, this will print
345       the headline of all entries in the database::
346
347           for e in Entry.objects.all():
348               print e.headline
349
350     * **Slicing.** As explained in `Limiting QuerySets`_ below, a ``QuerySet``
351       can be sliced, using Python's array-slicing syntax. Usually slicing a
352       ``QuerySet`` returns another (unevaluated )``QuerySet``, but Django will
353       execute the database query if you use the "step" parameter of slice
354       syntax.
355
356     * **repr().** A ``QuerySet`` is evaluated when you call ``repr()`` on it.
357       This is for convenience in the Python interactive interpreter, so you can
358       immediately see your results when using the API interactively.
359
360     * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it.
361       This, as you might expect, returns the length of the result list.
362
363       Note: *Don't* use ``len()`` on ``QuerySet``\s if all you want to do is
364       determine the number of records in the set. It's much more efficient to
365       handle a count at the database level, using SQL's ``SELECT COUNT(*)``,
366       and Django provides a ``count()`` method for precisely this reason. See
367       ``count()`` below.
368
369     * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on
370       it. For example::
371
372           entry_list = list(Entry.objects.all())
373
374       Be warned, though, that this could have a large memory overhead, because
375       Django will load each element of the list into memory. In contrast,
376       iterating over a ``QuerySet`` will take advantage of your database to
377       load data and instantiate objects only as you need them.
378
379
380 Pickling QuerySets
381 ~~~~~~~~~~~~~~~~~~
382
383 If you pickle_ a ``QuerySet``, this will also force all the results to be
384 loaded into memory prior to pickling. This is because pickling is usually used
385 as a precursor to caching and when the cached queryset is reloaded, you want
386 the results to already be present. This means that when you unpickle a
387 ``QuerySet``, it contains the results at the moment it was pickled, rather
388 than the results that are currently in the database.
389
390 If you only want to pickle the necessary information to recreate the
391 ``Queryset`` from the database at a later time, pickle the ``query`` attribute
392 of the ``QuerySet``. You can then recreate the original ``QuerySet`` (without
393 any results loaded) using some code like this::
394
395     >>> import pickle
396     >>> query = pickle.loads(s)     # Assuming 's' is the pickled string.
397     >>> qs = MyModel.objects.all()
398     >>> qs.query = query            # Restore the original 'query'.
399
400 .. _pickle: http://docs.python.org/lib/module-pickle.html
401
402 Limiting QuerySets
403 ------------------
404
405 Use Python's array-slicing syntax to limit your ``QuerySet`` to a certain
406 number of results. This is the equivalent of SQL's ``LIMIT`` and ``OFFSET``
407 clauses.
408
409 For example, this returns the first 5 objects (``LIMIT 5``)::
410
411     Entry.objects.all()[:5]
412
413 This returns the sixth through tenth objects (``OFFSET 5 LIMIT 5``)::
414
415     Entry.objects.all()[5:10]
416
417 You can also slice from the item ''N'' to the end of the queryset. For
418 example, to return everything from the sixth item onwards::
419
420     Entry.objects.all()[5:]
421
422 How this last example is implemented in SQL varies depending upon the database
423 used, but it is supported in all cases.
424
425 Generally, slicing a ``QuerySet`` returns a new ``QuerySet`` -- it doesn't
426 evaluate the query. An exception is if you use the "step" parameter of Python
427 slice syntax. For example, this would actually execute the query in order to
428 return a list of every *second* object of the first 10::
429
430     Entry.objects.all()[:10:2]
431
432 To retrieve a *single* object rather than a list
433 (e.g. ``SELECT foo FROM bar LIMIT 1``), use a simple index instead of a
434 slice. For example, this returns the first ``Entry`` in the database, after
435 ordering entries alphabetically by headline::
436
437     Entry.objects.order_by('headline')[0]
438
439 This is roughly equivalent to::
440
441     Entry.objects.order_by('headline')[0:1].get()
442
443 Note, however, that the first of these will raise ``IndexError`` while the
444 second will raise ``DoesNotExist`` if no objects match the given criteria.
445
446 QuerySet methods that return new QuerySets
447 ------------------------------------------
448
449 Django provides a range of ``QuerySet`` refinement methods that modify either
450 the types of results returned by the ``QuerySet`` or the way its SQL query is
451 executed.
452
453 ``filter(**kwargs)``
454 ~~~~~~~~~~~~~~~~~~~~
455
456 Returns a new ``QuerySet`` containing objects that match the given lookup
457 parameters.
458
459 The lookup parameters (``**kwargs``) should be in the format described in
460 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
461 underlying SQL statement.
462
463 ``exclude(**kwargs)``
464 ~~~~~~~~~~~~~~~~~~~~~
465
466 Returns a new ``QuerySet`` containing objects that do *not* match the given
467 lookup parameters.
468
469 The lookup parameters (``**kwargs``) should be in the format described in
470 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
471 underlying SQL statement, and the whole thing is enclosed in a ``NOT()``.
472
473 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
474 AND whose ``headline`` is "Hello"::
475
476     Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')
477
478 In SQL terms, that evaluates to::
479
480     SELECT ...
481     WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello')
482
483 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
484 OR whose headline is "Hello"::
485
486     Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello')
487
488 In SQL terms, that evaluates to::
489
490     SELECT ...
491     WHERE NOT pub_date > '2005-1-3'
492     AND NOT headline = 'Hello'
493
494 Note the second example is more restrictive.
495
496 ``order_by(*fields)``
497 ~~~~~~~~~~~~~~~~~~~~~
498
499 By default, results returned by a ``QuerySet`` are ordered by the ordering
500 tuple given by the ``ordering`` option in the model's ``Meta``. You can
501 override this on a per-``QuerySet`` basis by using the ``order_by`` method.
502
503 Example::
504
505     Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')
506
507 The result above will be ordered by ``pub_date`` descending, then by
508 ``headline`` ascending. The negative sign in front of ``"-pub_date"`` indicates
509 *descending* order. Ascending order is implied. To order randomly, use ``"?"``,
510 like so::
511
512     Entry.objects.order_by('?')
513
514 Note: ``order_by('?')`` queries may be expensive and slow, depending on the
515 database backend you're using.
516
517 To order by a field in a different model, use the same syntax as when you are
518 querying across model relations. That is, the name of the field, followed by a
519 double underscore (``__``), followed by the name of the field in the new model,
520 and so on for as many models as you want to join. For example::
521
522     Entry.objects.order_by('blog__name', 'headline')
523
524 If you try to order by a field that is a relation to another model, Django will
525 use the default ordering on the related model (or order by the related model's
526 primary key if there is no ``Meta.ordering`` specified. For example::
527
528     Entry.objects.order_by('blog')
529
530 ...is identical to::
531
532     Entry.objects.order_by('blog__id')
533
534 ...since the ``Blog`` model has no default ordering specified.
535
536 Be cautious when ordering by fields in related models if you are also using
537 ``distinct()``. See the note in the `distinct()`_ section for an explanation
538 of how related model ordering can change the expected results.
539
540 It is permissible to specify a multi-valued field to order the results by (for
541 example, a ``ManyToMany`` field). Normally this won't be a sensible thing to
542 do and it's really an advanced usage feature. However, if you know that your
543 queryset's filtering or available data implies that there will only be one
544 ordering piece of data for each of the main items you are selecting, the
545 ordering may well be exactly what you want to do. Use ordering on multi-valued
546 fields with care and make sure the results are what you expect.
547
548 **New in Django development version:** If you don't want any ordering to be
549 applied to a query, not even the default ordering, call ``order_by()`` with no
550 parameters.
551
552 **New in Django development version:** The syntax for ordering across related
553 models has changed. See the `Django 0.96 documentation`_ for the old behavior.
554
555 .. _Django 0.96 documentation: http://www.djangoproject.com/documentation/0.96/model-api/#floatfield
556
557 There's no way to specify whether ordering should be case sensitive. With
558 respect to case-sensitivity, Django will order results however your database
559 backend normally orders them.
560
561 ``reverse()``
562 ~~~~~~~~~~~~~
563
564 **New in Django development version**
565
566 Use the ``reverse()`` method to reverse the order in which a queryset's
567 elements are returned. Calling ``reverse()`` a second time restores the
568 ordering back to the normal direction.
569
570 To retrieve the ''last'' five items in a queryset, you could do this::
571
572     my_queryset.reverse()[:5]
573
574 Note that this is not quite the same as slicing from the end of a sequence in
575 Python. The above example will return the last item first, then the
576 penultimate item and so on. If we had a Python sequence and looked at
577 ``seq[:-5]``, we would see the fifth-last item first. Django doesn't support
578 that mode of access (slicing from the end), because it's not possible to do it
579 efficiently in SQL.
580
581 ``distinct()``
582 ~~~~~~~~~~~~~~
583
584 Returns a new ``QuerySet`` that uses ``SELECT DISTINCT`` in its SQL query. This
585 eliminates duplicate rows from the query results.
586
587 By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this
588 is rarely a problem, because simple queries such as ``Blog.objects.all()``
589 don't introduce the possibility of duplicate result rows. However, if your
590 query spans multiple tables, it's possible to get duplicate results when a
591 ``QuerySet`` is evaluated. That's when you'd use ``distinct()``.
592
593 .. note::
594     Any fields used in an ``order_by()`` call are included in the SQL
595     ``SELECT`` columns. This can sometimes lead to unexpected results when
596     used in conjunction with ``distinct()``. If you order by fields from a
597     related model, those fields will be added to the selected columns and they
598     may make otherwise duplicate rows appear to be distinct. Since the extra
599     columns don't appear in the returned results (they are only there to
600     support ordering), it sometimes looks like non-distinct results are being
601     returned.
602
603     Similarly, if you use a ``values()`` query to restrict the columns
604     selected, the columns used in any ``order_by()`` (or default model
605     ordering) will still be involved and may affect uniqueness of the results.
606
607     The moral here is that if you are using ``distinct()`` be careful about
608     ordering by related models. Similarly, when using ``distinct()`` and
609     ``values()`` together, be careful when ordering by fields not in the
610     ``values()`` call.
611
612 ``values(*fields)``
613 ~~~~~~~~~~~~~~~~~~~
614
615 Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of
616 dictionaries instead of model-instance objects.
617
618 Each of those dictionaries represents an object, with the keys corresponding to
619 the attribute names of model objects.
620
621 This example compares the dictionaries of ``values()`` with the normal model
622 objects::
623
624     # This list contains a Blog object.
625     >>> Blog.objects.filter(name__startswith='Beatles')
626     [Beatles Blog]
627
628     # This list contains a dictionary.
629     >>> Blog.objects.filter(name__startswith='Beatles').values()
630     [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]
631
632 ``values()`` takes optional positional arguments, ``*fields``, which specify
633 field names to which the ``SELECT`` should be limited. If you specify the
634 fields, each dictionary will contain only the field keys/values for the fields
635 you specify. If you don't specify the fields, each dictionary will contain a
636 key and value for every field in the database table.
637
638 Example::
639
640     >>> Blog.objects.values()
641     [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
642     >>> Blog.objects.values('id', 'name')
643     [{'id': 1, 'name': 'Beatles Blog'}]
644
645 You can also retrieve values from across ``ForeignKey`` relations by using
646 double underscores to separate the field names, just as when calling the
647 ``filter()`` command. For example::
648
649     >>> Entry.objects.values('blog__name').distinct()
650     [{'name': 'Beatles Blog'}]
651
652 A couple of subtleties that are worth mentioning:
653
654     * The ``values()`` method does not return anything for ``ManyToManyField``
655       attributes and will raise an error if you try to pass in this type of
656       field to it.
657     * If you have a field called ``foo`` that is a ``ForeignKey``, the default
658       ``values()`` call will return a dictionary key called ``foo_id``, since
659       this is the name of the hidden model attribute that stores the actual
660       value (the ``foo`` attribute refers to the related model). When you are
661       calling ``values()`` and passing in field names, you can pass in either
662       ``foo`` or ``foo_id`` and you will get back the same thing (the
663       dictionary key will match the field name you passed in).
664
665       For example::
666
667         >>> Entry.objects.values()
668         [{'blog_id: 1, 'headline': u'First Entry', ...}, ...]
669
670         >>> Entry.objects.values('blog')
671         [{'blog': 1}, ...]
672
673         >>> Entry.objects.values('blog_id')
674         [{'blog_id': 1}, ...]
675     * When using ``values()`` together with ``distinct()``, be aware that
676       ordering can affect the results. See the note in the `distinct()`_
677       section, above, for details.
678
679 **New in Django development version:** Previously, it was not possible to pass
680 ``blog_id`` to ``values()`` in the above example, only ``blog``.
681
682 A ``ValuesQuerySet`` is useful when you know you're only going to need values
683 from a small number of the available fields and you won't need the
684 functionality of a model instance object. It's more efficient to select only
685 the fields you need to use.
686
687 Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all
688 methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or
689 whatever. Yes, that means these two calls are identical::
690
691     Blog.objects.values().order_by('id')
692     Blog.objects.order_by('id').values()
693
694 The people who made Django prefer to put all the SQL-affecting methods first,
695 followed (optionally) by any output-affecting methods (such as ``values()``),
696 but it doesn't really matter. This is your chance to really flaunt your
697 individualism.
698
699 ``values_list(*fields)``
700 ~~~~~~~~~~~~~~~~~~~~~~~~
701
702 **New in Django development version**
703
704 This is similar to ``values()`` except that instead of returning a list of
705 dictionaries, it returns a list of tuples. Each tuple contains the value from
706 the respective field passed into the ``values_list()`` call -- so the first
707 item is the first field, etc. For example::
708
709     >>> Entry.objects.values_list('id', 'headline')
710     [(1, u'First entry'), ...]
711
712 If you only pass in a single field, you can also pass in the ``flat``
713 parameter. If ``True``, this will mean the returned results are single values,
714 rather than one-tuples. An example should make the difference clearer::
715
716     >>> Entry.objects.values_list('id').order_by('id')
717     [(1,), (2,), (3,), ...]
718
719     >>> Entry.objects.values_list('id', flat=True).order_by('id')
720     [1, 2, 3, ...]
721
722 It is an error to pass in ``flat`` when there is more than one field.
723
724 If you don't pass any values to ``values_list()``, it will return all the
725 fields in the model, in the order they were declared.
726
727 ``dates(field, kind, order='ASC')``
728 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
729
730 Returns a ``DateQuerySet`` -- a ``QuerySet`` that evaluates to a list of
731 ``datetime.datetime`` objects representing all available dates of a particular
732 kind within the contents of the ``QuerySet``.
733
734 ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your
735 model.
736
737 ``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each
738 ``datetime.datetime`` object in the result list is "truncated" to the given
739 ``type``.
740
741     * ``"year"`` returns a list of all distinct year values for the field.
742     * ``"month"`` returns a list of all distinct year/month values for the field.
743     * ``"day"`` returns a list of all distinct year/month/day values for the field.
744
745 ``order``, which defaults to ``'ASC'``, should be either ``'ASC'`` or
746 ``'DESC'``. This specifies how to order the results.
747
748 Examples::
749
750     >>> Entry.objects.dates('pub_date', 'year')
751     [datetime.datetime(2005, 1, 1)]
752     >>> Entry.objects.dates('pub_date', 'month')
753     [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)]
754     >>> Entry.objects.dates('pub_date', 'day')
755     [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)]
756     >>> Entry.objects.dates('pub_date', 'day', order='DESC')
757     [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)]
758     >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day')
759     [datetime.datetime(2005, 3, 20)]
760
761 ``none()``
762 ~~~~~~~~~~
763
764 **New in Django development version**
765
766 Returns an ``EmptyQuerySet`` -- a ``QuerySet`` that always evaluates to
767 an empty list. This can be used in cases where you know that you should
768 return an empty result set and your caller is expecting a ``QuerySet``
769 object (instead of returning an empty list, for example.)
770
771 Examples::
772
773     >>> Entry.objects.none()
774     []
775
776 ``all()``
777 ~~~~~~~~~~
778
779 **New in Django development version**
780
781 Returns a ''copy'' of the current ``QuerySet`` (or ``QuerySet`` subclass you
782 pass in). This can be useful in some situations where you might want to pass
783 in either a model manager or a ``QuerySet`` and do further filtering on the
784 result. You can safely call ``all()`` on either object and then you'll
785 definitely have a ``QuerySet`` to work with.
786
787 ``select_related()``
788 ~~~~~~~~~~~~~~~~~~~~
789
790 Returns a ``QuerySet`` that will automatically "follow" foreign-key
791 relationships, selecting that additional related-object data when it executes
792 its query. This is a performance booster which results in (sometimes much)
793 larger queries but means later use of foreign-key relationships won't require
794 database queries.
795
796 The following examples illustrate the difference between plain lookups and
797 ``select_related()`` lookups. Here's standard lookup::
798
799     # Hits the database.
800     e = Entry.objects.get(id=5)
801
802     # Hits the database again to get the related Blog object.
803     b = e.blog
804
805 And here's ``select_related`` lookup::
806
807     # Hits the database.
808     e = Entry.objects.select_related().get(id=5)
809
810     # Doesn't hit the database, because e.blog has been prepopulated
811     # in the previous query.
812     b = e.blog
813
814 ``select_related()`` follows foreign keys as far as possible. If you have the
815 following models::
816
817     class City(models.Model):
818         # ...
819
820     class Person(models.Model):
821         # ...
822         hometown = models.ForeignKey(City)
823
824     class Book(models.Model):
825         # ...
826         author = models.ForeignKey(Person)
827
828 ...then a call to ``Book.objects.select_related().get(id=4)`` will cache the
829 related ``Person`` *and* the related ``City``::
830
831     b = Book.objects.select_related().get(id=4)
832     p = b.author         # Doesn't hit the database.
833     c = p.hometown       # Doesn't hit the database.
834
835     b = Book.objects.get(id=4) # No select_related() in this example.
836     p = b.author         # Hits the database.
837     c = p.hometown       # Hits the database.
838
839 Note that, by default, ``select_related()`` does not follow foreign keys that
840 have ``null=True``.
841
842 Usually, using ``select_related()`` can vastly improve performance because your
843 app can avoid many database calls. However, in situations with deeply nested
844 sets of relationships ``select_related()`` can sometimes end up following "too
845 many" relations, and can generate queries so large that they end up being slow.
846
847 In these situations, you can use the ``depth`` argument to ``select_related()``
848 to control how many "levels" of relations ``select_related()`` will actually
849 follow::
850
851     b = Book.objects.select_related(depth=1).get(id=4)
852     p = b.author         # Doesn't hit the database.
853     c = p.hometown       # Requires a database call.
854
855 The ``depth`` argument is new in the Django development version.
856
857 **New in Django development version:** Sometimes you only need to access
858 specific models that are related to your root model, not all of the related
859 models. In these cases, you can pass the related field names to
860 ``select_related()`` and it will only follow those relations. You can even do
861 this for models that are more than one relation away by separating the field
862 names with double underscores, just as for filters. For example, if we have
863 this model::
864
865     class Room(models.Model):
866         # ...
867         building = models.ForeignKey(...)
868
869     class Group(models.Model):
870         # ...
871         teacher = models.ForeignKey(...)
872         room = models.ForeignKey(Room)
873         subject = models.ForeignKey(...)
874
875 ...and we only needed to work with the ``room`` and ``subject`` attributes, we
876 could write this::
877
878     g = Group.objects.select_related('room', 'subject')
879
880 This is also valid::
881
882     g = Group.objects.select_related('room__building', 'subject')
883
884 ...and would also pull in the ``building`` relation.
885
886 You can only refer to ``ForeignKey`` relations in the list of fields passed to
887 ``select_related``. You *can* refer to foreign keys that have ``null=True``
888 (unlike the default ``select_related()`` call). It's an error to use both a
889 list of fields and the ``depth`` parameter in the same ``select_related()``
890 call, since they are conflicting options.
891
892 ``extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)``
893 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
894
895 Sometimes, the Django query syntax by itself can't easily express a complex
896 ``WHERE`` clause. For these edge cases, Django provides the ``extra()``
897 ``QuerySet`` modifier -- a hook for injecting specific clauses into the SQL
898 generated by a ``QuerySet``.
899
900 By definition, these extra lookups may not be portable to different database
901 engines (because you're explicitly writing SQL code) and violate the DRY
902 principle, so you should avoid them if possible.
903
904 Specify one or more of ``params``, ``select``, ``where`` or ``tables``. None
905 of the arguments is required, but you should use at least one of them.
906
907 ``select``
908     The ``select`` argument lets you put extra fields in the ``SELECT`` clause.
909     It should be a dictionary mapping attribute names to SQL clauses to use to
910     calculate that attribute.
911
912     Example::
913
914         Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
915
916     As a result, each ``Entry`` object will have an extra attribute,
917     ``is_recent``, a boolean representing whether the entry's ``pub_date`` is
918     greater than Jan. 1, 2006.
919
920     Django inserts the given SQL snippet directly into the ``SELECT``
921     statement, so the resulting SQL of the above example would be::
922
923         SELECT blog_entry.*, (pub_date > '2006-01-01')
924         FROM blog_entry;
925
926
927     The next example is more advanced; it does a subquery to give each
928     resulting ``Blog`` object an ``entry_count`` attribute, an integer count
929     of associated ``Entry`` objects::
930
931         Blog.objects.extra(
932             select={
933                 'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
934             },
935         )
936
937     (In this particular case, we're exploiting the fact that the query will
938     already contain the ``blog_blog`` table in its ``FROM`` clause.)
939
940     The resulting SQL of the above example would be::
941
942         SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id)
943         FROM blog_blog;
944
945     Note that the parenthesis required by most database engines around
946     subqueries are not required in Django's ``select`` clauses. Also note that
947     some database backends, such as some MySQL versions, don't support
948     subqueries.
949
950     **New in Django development version**
951     In some rare cases, you might wish to pass parameters to the SQL fragments
952     in ``extra(select=...)```. For this purpose, use the ``select_params``
953     parameter. Since ``select_params`` is a sequence and the ``select``
954     attribute is a dictionary, some care is required so that the parameters
955     are matched up correctly with the extra select pieces.  In this situation,
956     you should use a ``django.utils.datastructures.SortedDict`` for the
957     ``select`` value, not just a normal Python dictionary.
958
959     This will work, for example::
960
961         Blog.objects.extra(
962             select=SortedDict(('a', '%s'), ('b', '%s')),
963             select_params=('one', 'two'))
964
965 ``where`` / ``tables``
966     You can define explicit SQL ``WHERE`` clauses -- perhaps to perform
967     non-explicit joins -- by using ``where``. You can manually add tables to
968     the SQL ``FROM`` clause by using ``tables``.
969
970     ``where`` and ``tables`` both take a list of strings. All ``where``
971     parameters are "AND"ed to any other search criteria.
972
973     Example::
974
975         Entry.objects.extra(where=['id IN (3, 4, 5, 20)'])
976
977     ...translates (roughly) into the following SQL::
978
979         SELECT * FROM blog_entry WHERE id IN (3, 4, 5, 20);
980
981     Be careful when using the ``tables`` parameter if you're specifying
982     tables that are already used in the query. When you add extra tables
983     via the ``tables`` parameter, Django assumes you want that table included
984     an extra time, if it is already included. That creates a problem,
985     since the table name will then be given an alias. If a table appears
986     multiple times in an SQL statement, the second and subsequent occurrences
987     must use aliases so the database can tell them apart. If you're
988     referring to the extra table you added in the extra ``where`` parameter
989     this is going to cause errors.
990
991     Normally you'll only be adding extra tables that don't already appear in
992     the query. However, if the case outlined above does occur, there are a few
993     solutions. First, see if you can get by without including the extra table
994     and use the one already in the query. If that isn't possible, put your
995     ``extra()`` call at the front of the queryset construction so that your
996     table is the first use of that table. Finally, if all else fails, look at
997     the query produced and rewrite your ``where`` addition to use the alias
998     given to your extra table. The alias will be the same each time you
999     construct the queryset in the same way, so you can rely upon the alias
1000     name to not change.
1001
1002 ``order_by``
1003     If you need to order the resulting queryset using some of the new fields
1004     or tables you have included via ``extra()`` use the ``order_by`` parameter
1005     to ``extra()`` and pass in a sequence of strings. These strings should
1006     either be model fields (as in the normal ``order_by()`` method on
1007     querysets), of the form ``table_name.column_name`` or an alias for a column
1008     that you specified in the ``select`` parameter to ``extra()``.
1009
1010     For example::
1011
1012         q = Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
1013         q = q.extra(order_by = ['-is_recent'])
1014
1015     This would sort all the items for which ``is_recent`` is true to the front
1016     of the result set (``True`` sorts before ``False`` in a descending
1017     ordering).
1018
1019     This shows, by the way, that you can make multiple calls to
1020     ``extra()`` and it will behave as you expect (adding new constraints each
1021     time).
1022
1023 ``params``
1024     The ``where`` parameter described above may use standard Python database
1025     string placeholders -- ``'%s'`` to indicate parameters the database engine
1026     should automatically quote. The ``params`` argument is a list of any extra
1027     parameters to be substituted.
1028
1029     Example::
1030
1031         Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1032
1033     Always use ``params`` instead of embedding values directly into ``where``
1034     because ``params`` will ensure values are quoted correctly according to
1035     your particular backend. (For example, quotes will be escaped correctly.)
1036
1037     Bad::
1038
1039         Entry.objects.extra(where=["headline='Lennon'"])
1040
1041     Good::
1042
1043         Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1044
1045 **New in Django development version** The ``select_params`` argument to
1046 ``extra()`` is new. Previously, you could attempt to pass parameters for
1047 ``select`` in the ``params`` argument, but it worked very unreliably.
1048
1049 QuerySet methods that do not return QuerySets
1050 ---------------------------------------------
1051
1052 The following ``QuerySet`` methods evaluate the ``QuerySet`` and return
1053 something *other than* a ``QuerySet``.
1054
1055 These methods do not use a cache (see `Caching and QuerySets`_ below). Rather,
1056 they query the database each time they're called.
1057
1058 ``get(**kwargs)``
1059 ~~~~~~~~~~~~~~~~~
1060
1061 Returns the object matching the given lookup parameters, which should be in
1062 the format described in `Field lookups`_.
1063
1064 ``get()`` raises ``AssertionError`` if more than one object was found.
1065
1066 ``get()`` raises a ``DoesNotExist`` exception if an object wasn't found for the
1067 given parameters. The ``DoesNotExist`` exception is an attribute of the model
1068 class. Example::
1069
1070     Entry.objects.get(id='foo') # raises Entry.DoesNotExist
1071
1072 The ``DoesNotExist`` exception inherits from
1073 ``django.core.exceptions.ObjectDoesNotExist``, so you can target multiple
1074 ``DoesNotExist`` exceptions. Example::
1075
1076     from django.core.exceptions import ObjectDoesNotExist
1077     try:
1078         e = Entry.objects.get(id=3)
1079         b = Blog.objects.get(id=1)
1080     except ObjectDoesNotExist:
1081         print "Either the entry or blog doesn't exist."
1082
1083 ``create(**kwargs)``
1084 ~~~~~~~~~~~~~~~~~~~~
1085
1086 A convenience method for creating an object and saving it all in one step.  Thus::
1087
1088     p = Person.objects.create(first_name="Bruce", last_name="Springsteen")
1089
1090 and::
1091
1092     p = Person(first_name="Bruce", last_name="Springsteen")
1093     p.save()
1094
1095 are equivalent.
1096
1097 ``get_or_create(**kwargs)``
1098 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1099
1100 A convenience method for looking up an object with the given kwargs, creating
1101 one if necessary.
1102
1103 Returns a tuple of ``(object, created)``, where ``object`` is the retrieved or
1104 created object and ``created`` is a boolean specifying whether a new object was
1105 created.
1106
1107 This is meant as a shortcut to boilerplatish code and is mostly useful for
1108 data-import scripts. For example::
1109
1110     try:
1111         obj = Person.objects.get(first_name='John', last_name='Lennon')
1112     except Person.DoesNotExist:
1113         obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
1114         obj.save()
1115
1116 This pattern gets quite unwieldy as the number of fields in a model goes up.
1117 The above example can be rewritten using ``get_or_create()`` like so::
1118
1119     obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon',
1120                       defaults={'birthday': date(1940, 10, 9)})
1121
1122 Any keyword arguments passed to ``get_or_create()`` -- *except* an optional one
1123 called ``defaults`` -- will be used in a ``get()`` call. If an object is found,
1124 ``get_or_create()`` returns a tuple of that object and ``False``. If an object
1125 is *not* found, ``get_or_create()`` will instantiate and save a new object,
1126 returning a tuple of the new object and ``True``. The new object will be
1127 created according to this algorithm::
1128
1129     defaults = kwargs.pop('defaults', {})
1130     params = dict([(k, v) for k, v in kwargs.items() if '__' not in k])
1131     params.update(defaults)
1132     obj = self.model(**params)
1133   &nbs