Django

Code

root/django/branches/gis/docs/db-api.txt

Revision 8215, 88.0 kB (checked in by jbronn, 4 months ago)

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

  • 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 Combining QuerySets
447 -------------------
448
449 If you have two ``QuerySet`` instances that act on the same model, you can
450 combine them using ``&`` and ``|`` to get the items that are in both result
451 sets or in either results set, respectively. For example::
452
453     Entry.objects.filter(pubdate__gte=date1) & \
454             Entry.objects.filter(headline__startswith="What")
455
456 will combine the two queries into a single SQL query. Of course, in this case
457 you could have achieved the same result using multiple filters on the same
458 ``QuerySet``, but sometimes the ability to combine individual ``QuerySet``
459 instance is useful.
460
461 Be careful, if you are using ``extra()`` to add custom handling to your
462 ``QuerySet`` however. All the ``extra()`` components are merged and the result
463 may or may not make sense. If you are using custom SQL fragments in your
464 ``extra()`` calls, Django will not inspect these fragments to see if they need
465 to be rewritten because of changes in the merged query. So test the effects
466 carefully. Also realize that if you are combining two ``QuerySets`` with
467 ``|``, you cannot use ``extra(select=...)`` or ``extra(where=...)`` on *both*
468 ``QuerySets``. You can only use those calls on one or the other (Django will
469 raise a ``ValueError`` if you try to use this incorrectly).
470
471 QuerySet methods that return new QuerySets
472 ------------------------------------------
473
474 Django provides a range of ``QuerySet`` refinement methods that modify either
475 the types of results returned by the ``QuerySet`` or the way its SQL query is
476 executed.
477
478 ``filter(**kwargs)``
479 ~~~~~~~~~~~~~~~~~~~~
480
481 Returns a new ``QuerySet`` containing objects that match the given lookup
482 parameters.
483
484 The lookup parameters (``**kwargs``) should be in the format described in
485 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
486 underlying SQL statement.
487
488 ``exclude(**kwargs)``
489 ~~~~~~~~~~~~~~~~~~~~~
490
491 Returns a new ``QuerySet`` containing objects that do *not* match the given
492 lookup parameters.
493
494 The lookup parameters (``**kwargs``) should be in the format described in
495 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the
496 underlying SQL statement, and the whole thing is enclosed in a ``NOT()``.
497
498 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
499 AND whose ``headline`` is "Hello"::
500
501     Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello')
502
503 In SQL terms, that evaluates to::
504
505     SELECT ...
506     WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello')
507
508 This example excludes all entries whose ``pub_date`` is later than 2005-1-3
509 OR whose headline is "Hello"::
510
511     Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello')
512
513 In SQL terms, that evaluates to::
514
515     SELECT ...
516     WHERE NOT pub_date > '2005-1-3'
517     AND NOT headline = 'Hello'
518
519 Note the second example is more restrictive.
520
521 ``order_by(*fields)``
522 ~~~~~~~~~~~~~~~~~~~~~
523
524 By default, results returned by a ``QuerySet`` are ordered by the ordering
525 tuple given by the ``ordering`` option in the model's ``Meta``. You can
526 override this on a per-``QuerySet`` basis by using the ``order_by`` method.
527
528 Example::
529
530     Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline')
531
532 The result above will be ordered by ``pub_date`` descending, then by
533 ``headline`` ascending. The negative sign in front of ``"-pub_date"`` indicates
534 *descending* order. Ascending order is implied. To order randomly, use ``"?"``,
535 like so::
536
537     Entry.objects.order_by('?')
538
539 Note: ``order_by('?')`` queries may be expensive and slow, depending on the
540 database backend you're using.
541
542 To order by a field in a different model, use the same syntax as when you are
543 querying across model relations. That is, the name of the field, followed by a
544 double underscore (``__``), followed by the name of the field in the new model,
545 and so on for as many models as you want to join. For example::
546
547     Entry.objects.order_by('blog__name', 'headline')
548
549 If you try to order by a field that is a relation to another model, Django will
550 use the default ordering on the related model (or order by the related model's
551 primary key if there is no ``Meta.ordering`` specified. For example::
552
553     Entry.objects.order_by('blog')
554
555 ...is identical to::
556
557     Entry.objects.order_by('blog__id')
558
559 ...since the ``Blog`` model has no default ordering specified.
560
561 Be cautious when ordering by fields in related models if you are also using
562 ``distinct()``. See the note in the `distinct()`_ section for an explanation
563 of how related model ordering can change the expected results.
564
565 It is permissible to specify a multi-valued field to order the results by (for
566 example, a ``ManyToMany`` field). Normally this won't be a sensible thing to
567 do and it's really an advanced usage feature. However, if you know that your
568 queryset's filtering or available data implies that there will only be one
569 ordering piece of data for each of the main items you are selecting, the
570 ordering may well be exactly what you want to do. Use ordering on multi-valued
571 fields with care and make sure the results are what you expect.
572
573 **New in Django development version:** If you don't want any ordering to be
574 applied to a query, not even the default ordering, call ``order_by()`` with no
575 parameters.
576
577 **New in Django development version:** The syntax for ordering across related
578 models has changed. See the `Django 0.96 documentation`_ for the old behavior.
579
580 .. _Django 0.96 documentation: http://www.djangoproject.com/documentation/0.96/model-api/#floatfield
581
582 There's no way to specify whether ordering should be case sensitive. With
583 respect to case-sensitivity, Django will order results however your database
584 backend normally orders them.
585
586 ``reverse()``
587 ~~~~~~~~~~~~~
588
589 **New in Django development version**
590
591 Use the ``reverse()`` method to reverse the order in which a queryset's
592 elements are returned. Calling ``reverse()`` a second time restores the
593 ordering back to the normal direction.
594
595 To retrieve the ''last'' five items in a queryset, you could do this::
596
597     my_queryset.reverse()[:5]
598
599 Note that this is not quite the same as slicing from the end of a sequence in
600 Python. The above example will return the last item first, then the
601 penultimate item and so on. If we had a Python sequence and looked at
602 ``seq[:-5]``, we would see the fifth-last item first. Django doesn't support
603 that mode of access (slicing from the end), because it's not possible to do it
604 efficiently in SQL.
605
606 ``distinct()``
607 ~~~~~~~~~~~~~~
608
609 Returns a new ``QuerySet`` that uses ``SELECT DISTINCT`` in its SQL query. This
610 eliminates duplicate rows from the query results.
611
612 By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this
613 is rarely a problem, because simple queries such as ``Blog.objects.all()``
614 don't introduce the possibility of duplicate result rows. However, if your
615 query spans multiple tables, it's possible to get duplicate results when a
616 ``QuerySet`` is evaluated. That's when you'd use ``distinct()``.
617
618 .. note::
619     Any fields used in an ``order_by()`` call are included in the SQL
620     ``SELECT`` columns. This can sometimes lead to unexpected results when
621     used in conjunction with ``distinct()``. If you order by fields from a
622     related model, those fields will be added to the selected columns and they
623     may make otherwise duplicate rows appear to be distinct. Since the extra
624     columns don't appear in the returned results (they are only there to
625     support ordering), it sometimes looks like non-distinct results are being
626     returned.
627
628     Similarly, if you use a ``values()`` query to restrict the columns
629     selected, the columns used in any ``order_by()`` (or default model
630     ordering) will still be involved and may affect uniqueness of the results.
631
632     The moral here is that if you are using ``distinct()`` be careful about
633     ordering by related models. Similarly, when using ``distinct()`` and
634     ``values()`` together, be careful when ordering by fields not in the
635     ``values()`` call.
636
637 ``values(*fields)``
638 ~~~~~~~~~~~~~~~~~~~
639
640 Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of
641 dictionaries instead of model-instance objects.
642
643 Each of those dictionaries represents an object, with the keys corresponding to
644 the attribute names of model objects.
645
646 This example compares the dictionaries of ``values()`` with the normal model
647 objects::
648
649     # This list contains a Blog object.
650     >>> Blog.objects.filter(name__startswith='Beatles')
651     [Beatles Blog]
652
653     # This list contains a dictionary.
654     >>> Blog.objects.filter(name__startswith='Beatles').values()
655     [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}]
656
657 ``values()`` takes optional positional arguments, ``*fields``, which specify
658 field names to which the ``SELECT`` should be limited. If you specify the
659 fields, each dictionary will contain only the field keys/values for the fields
660 you specify. If you don't specify the fields, each dictionary will contain a
661 key and value for every field in the database table.
662
663 Example::
664
665     >>> Blog.objects.values()
666     [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],
667     >>> Blog.objects.values('id', 'name')
668     [{'id': 1, 'name': 'Beatles Blog'}]
669
670 You can also retrieve values from across ``ForeignKey`` relations by using
671 double underscores to separate the field names, just as when calling the
672 ``filter()`` command. For example::
673
674     >>> Entry.objects.values('blog__name').distinct()
675     [{'name': 'Beatles Blog'}]
676
677 A couple of subtleties that are worth mentioning:
678
679     * The ``values()`` method does not return anything for ``ManyToManyField``
680       attributes and will raise an error if you try to pass in this type of
681       field to it.
682     * If you have a field called ``foo`` that is a ``ForeignKey``, the default
683       ``values()`` call will return a dictionary key called ``foo_id``, since
684       this is the name of the hidden model attribute that stores the actual
685       value (the ``foo`` attribute refers to the related model). When you are
686       calling ``values()`` and passing in field names, you can pass in either
687       ``foo`` or ``foo_id`` and you will get back the same thing (the
688       dictionary key will match the field name you passed in).
689
690       For example::
691
692         >>> Entry.objects.values()
693         [{'blog_id: 1, 'headline': u'First Entry', ...}, ...]
694
695         >>> Entry.objects.values('blog')
696         [{'blog': 1}, ...]
697
698         >>> Entry.objects.values('blog_id')
699         [{'blog_id': 1}, ...]
700     * When using ``values()`` together with ``distinct()``, be aware that
701       ordering can affect the results. See the note in the `distinct()`_
702       section, above, for details.
703
704 **New in Django development version:** Previously, it was not possible to pass
705 ``blog_id`` to ``values()`` in the above example, only ``blog``.
706
707 A ``ValuesQuerySet`` is useful when you know you're only going to need values
708 from a small number of the available fields and you won't need the
709 functionality of a model instance object. It's more efficient to select only
710 the fields you need to use.
711
712 Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all
713 methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or
714 whatever. Yes, that means these two calls are identical::
715
716     Blog.objects.values().order_by('id')
717     Blog.objects.order_by('id').values()
718
719 The people who made Django prefer to put all the SQL-affecting methods first,
720 followed (optionally) by any output-affecting methods (such as ``values()``),
721 but it doesn't really matter. This is your chance to really flaunt your
722 individualism.
723
724 ``values_list(*fields)``
725 ~~~~~~~~~~~~~~~~~~~~~~~~
726
727 **New in Django development version**
728
729 This is similar to ``values()`` except that instead of returning a list of
730 dictionaries, it returns a list of tuples. Each tuple contains the value from
731 the respective field passed into the ``values_list()`` call -- so the first
732 item is the first field, etc. For example::
733
734     >>> Entry.objects.values_list('id', 'headline')
735     [(1, u'First entry'), ...]
736
737 If you only pass in a single field, you can also pass in the ``flat``
738 parameter. If ``True``, this will mean the returned results are single values,
739 rather than one-tuples. An example should make the difference clearer::
740
741     >>> Entry.objects.values_list('id').order_by('id')
742     [(1,), (2,), (3,), ...]
743
744     >>> Entry.objects.values_list('id', flat=True).order_by('id')
745     [1, 2, 3, ...]
746
747 It is an error to pass in ``flat`` when there is more than one field.
748
749 If you don't pass any values to ``values_list()``, it will return all the
750 fields in the model, in the order they were declared.
751
752 ``dates(field, kind, order='ASC')``
753 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
754
755 Returns a ``DateQuerySet`` -- a ``QuerySet`` that evaluates to a list of
756 ``datetime.datetime`` objects representing all available dates of a particular
757 kind within the contents of the ``QuerySet``.
758
759 ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your
760 model.
761
762 ``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each
763 ``datetime.datetime`` object in the result list is "truncated" to the given
764 ``type``.
765
766     * ``"year"`` returns a list of all distinct year values for the field.
767     * ``"month"`` returns a list of all distinct year/month values for the field.
768     * ``"day"`` returns a list of all distinct year/month/day values for the field.
769
770 ``order``, which defaults to ``'ASC'``, should be either ``'ASC'`` or
771 ``'DESC'``. This specifies how to order the results.
772
773 Examples::
774
775     >>> Entry.objects.dates('pub_date', 'year')
776     [datetime.datetime(2005, 1, 1)]
777     >>> Entry.objects.dates('pub_date', 'month')
778     [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)]
779     >>> Entry.objects.dates('pub_date', 'day')
780     [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)]
781     >>> Entry.objects.dates('pub_date', 'day', order='DESC')
782     [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)]
783     >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day')
784     [datetime.datetime(2005, 3, 20)]
785
786 ``none()``
787 ~~~~~~~~~~
788
789 **New in Django development version**
790
791 Returns an ``EmptyQuerySet`` -- a ``QuerySet`` that always evaluates to
792 an empty list. This can be used in cases where you know that you should
793 return an empty result set and your caller is expecting a ``QuerySet``
794 object (instead of returning an empty list, for example.)
795
796 Examples::
797
798     >>> Entry.objects.none()
799     []
800
801 ``all()``
802 ~~~~~~~~~~
803
804 **New in Django development version**
805
806 Returns a ''copy'' of the current ``QuerySet`` (or ``QuerySet`` subclass you
807 pass in). This can be useful in some situations where you might want to pass
808 in either a model manager or a ``QuerySet`` and do further filtering on the
809 result. You can safely call ``all()`` on either object and then you'll
810 definitely have a ``QuerySet`` to work with.
811
812 ``select_related()``
813 ~~~~~~~~~~~~~~~~~~~~
814
815 Returns a ``QuerySet`` that will automatically "follow" foreign-key
816 relationships, selecting that additional related-object data when it executes
817 its query. This is a performance booster which results in (sometimes much)
818 larger queries but means later use of foreign-key relationships won't require
819 database queries.
820
821 The following examples illustrate the difference between plain lookups and
822 ``select_related()`` lookups. Here's standard lookup::
823
824     # Hits the database.
825     e = Entry.objects.get(id=5)
826
827     # Hits the database again to get the related Blog object.
828     b = e.blog
829
830 And here's ``select_related`` lookup::
831
832     # Hits the database.
833     e = Entry.objects.select_related().get(id=5)
834
835     # Doesn't hit the database, because e.blog has been prepopulated
836     # in the previous query.
837     b = e.blog
838
839 ``select_related()`` follows foreign keys as far as possible. If you have the
840 following models::
841
842     class City(models.Model):
843         # ...
844
845     class Person(models.Model):
846         # ...
847         hometown = models.ForeignKey(City)
848
849     class Book(models.Model):
850         # ...
851         author = models.ForeignKey(Person)
852
853 ...then a call to ``Book.objects.select_related().get(id=4)`` will cache the
854 related ``Person`` *and* the related ``City``::
855
856     b = Book.objects.select_related().get(id=4)
857     p = b.author         # Doesn't hit the database.
858     c = p.hometown       # Doesn't hit the database.
859
860     b = Book.objects.get(id=4) # No select_related() in this example.
861     p = b.author         # Hits the database.
862     c = p.hometown       # Hits the database.
863
864 Note that, by default, ``select_related()`` does not follow foreign keys that
865 have ``null=True``.
866
867 Usually, using ``select_related()`` can vastly improve performance because your
868 app can avoid many database calls. However, in situations with deeply nested
869 sets of relationships ``select_related()`` can sometimes end up following "too
870 many" relations, and can generate queries so large that they end up being slow.
871
872 In these situations, you can use the ``depth`` argument to ``select_related()``
873 to control how many "levels" of relations ``select_related()`` will actually
874 follow::
875
876     b = Book.objects.select_related(depth=1).get(id=4)
877     p = b.author         # Doesn't hit the database.
878     c = p.hometown       # Requires a database call.
879
880 The ``depth`` argument is new in the Django development version.
881
882 **New in Django development version:** Sometimes you only need to access
883 specific models that are related to your root model, not all of the related
884 models. In these cases, you can pass the related field names to
885 ``select_related()`` and it will only follow those relations. You can even do
886 this for models that are more than one relation away by separating the field
887 names with double underscores, just as for filters. For example, if we have
888 this model::
889
890     class Room(models.Model):
891         # ...
892         building = models.ForeignKey(...)
893
894     class Group(models.Model):
895         # ...
896         teacher = models.ForeignKey(...)
897         room = models.ForeignKey(Room)
898         subject = models.ForeignKey(...)
899
900 ...and we only needed to work with the ``room`` and ``subject`` attributes, we
901 could write this::
902
903     g = Group.objects.select_related('room', 'subject')
904
905 This is also valid::
906
907     g = Group.objects.select_related('room__building', 'subject')
908
909 ...and would also pull in the ``building`` relation.
910
911 You can only refer to ``ForeignKey`` relations in the list of fields passed to
912 ``select_related``. You *can* refer to foreign keys that have ``null=True``
913 (unlike the default ``select_related()`` call). It's an error to use both a
914 list of fields and the ``depth`` parameter in the same ``select_related()``
915 call, since they are conflicting options.
916
917 ``extra(select=None, where=None, params=None, tables=None, order_by=None, select_params=None)``
918 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
919
920 Sometimes, the Django query syntax by itself can't easily express a complex
921 ``WHERE`` clause. For these edge cases, Django provides the ``extra()``
922 ``QuerySet`` modifier -- a hook for injecting specific clauses into the SQL
923 generated by a ``QuerySet``.
924
925 By definition, these extra lookups may not be portable to different database
926 engines (because you're explicitly writing SQL code) and violate the DRY
927 principle, so you should avoid them if possible.
928
929 Specify one or more of ``params``, ``select``, ``where`` or ``tables``. None
930 of the arguments is required, but you should use at least one of them.
931
932 ``select``
933     The ``select`` argument lets you put extra fields in the ``SELECT`` clause.
934     It should be a dictionary mapping attribute names to SQL clauses to use to
935     calculate that attribute.
936
937     Example::
938
939         Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
940
941     As a result, each ``Entry`` object will have an extra attribute,
942     ``is_recent``, a boolean representing whether the entry's ``pub_date`` is
943     greater than Jan. 1, 2006.
944
945     Django inserts the given SQL snippet directly into the ``SELECT``
946     statement, so the resulting SQL of the above example would be::
947
948         SELECT blog_entry.*, (pub_date > '2006-01-01')
949         FROM blog_entry;
950
951
952     The next example is more advanced; it does a subquery to give each
953     resulting ``Blog`` object an ``entry_count`` attribute, an integer count
954     of associated ``Entry`` objects::
955
956         Blog.objects.extra(
957             select={
958                 'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id'
959             },
960         )
961
962     (In this particular case, we're exploiting the fact that the query will
963     already contain the ``blog_blog`` table in its ``FROM`` clause.)
964
965     The resulting SQL of the above example would be::
966
967         SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id)
968         FROM blog_blog;
969
970     Note that the parenthesis required by most database engines around
971     subqueries are not required in Django's ``select`` clauses. Also note that
972     some database backends, such as some MySQL versions, don't support
973     subqueries.
974
975     **New in Django development version**
976     In some rare cases, you might wish to pass parameters to the SQL fragments
977     in ``extra(select=...)```. For this purpose, use the ``select_params``
978     parameter. Since ``select_params`` is a sequence and the ``select``
979     attribute is a dictionary, some care is required so that the parameters
980     are matched up correctly with the extra select pieces.  In this situation,
981     you should use a ``django.utils.datastructures.SortedDict`` for the
982     ``select`` value, not just a normal Python dictionary.
983
984     This will work, for example::
985
986         Blog.objects.extra(
987             select=SortedDict(('a', '%s'), ('b', '%s')),
988             select_params=('one', 'two'))
989
990 ``where`` / ``tables``
991     You can define explicit SQL ``WHERE`` clauses -- perhaps to perform
992     non-explicit joins -- by using ``where``. You can manually add tables to
993     the SQL ``FROM`` clause by using ``tables``.
994
995     ``where`` and ``tables`` both take a list of strings. All ``where``
996     parameters are "AND"ed to any other search criteria.
997
998     Example::
999
1000         Entry.objects.extra(where=['id IN (3, 4, 5, 20)'])
1001
1002     ...translates (roughly) into the following SQL::
1003
1004         SELECT * FROM blog_entry WHERE id IN (3, 4, 5, 20);
1005
1006     Be careful when using the ``tables`` parameter if you're specifying
1007     tables that are already used in the query. When you add extra tables
1008     via the ``tables`` parameter, Django assumes you want that table included
1009     an extra time, if it is already included. That creates a problem,
1010     since the table name will then be given an alias. If a table appears
1011     multiple times in an SQL statement, the second and subsequent occurrences
1012     must use aliases so the database can tell them apart. If you're
1013     referring to the extra table you added in the extra ``where`` parameter
1014     this is going to cause errors.
1015
1016     Normally you'll only be adding extra tables that don't already appear in
1017     the query. However, if the case outlined above does occur, there are a few
1018     solutions. First, see if you can get by without including the extra table
1019     and use the one already in the query. If that isn't possible, put your
1020     ``extra()`` call at the front of the queryset construction so that your
1021     table is the first use of that table. Finally, if all else fails, look at
1022     the query produced and rewrite your ``where`` addition to use the alias
1023     given to your extra table. The alias will be the same each time you
1024     construct the queryset in the same way, so you can rely upon the alias
1025     name to not change.
1026
1027 ``order_by``
1028     If you need to order the resulting queryset using some of the new fields
1029     or tables you have included via ``extra()`` use the ``order_by`` parameter
1030     to ``extra()`` and pass in a sequence of strings. These strings should
1031     either be model fields (as in the normal ``order_by()`` method on
1032     querysets), of the form ``table_name.column_name`` or an alias for a column
1033     that you specified in the ``select`` parameter to ``extra()``.
1034
1035     For example::
1036
1037         q = Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"})
1038         q = q.extra(order_by = ['-is_recent'])
1039
1040     This would sort all the items for which ``is_recent`` is true to the front
1041     of the result set (``True`` sorts before ``False`` in a descending
1042     ordering).
1043
1044     This shows, by the way, that you can make multiple calls to
1045     ``extra()`` and it will behave as you expect (adding new constraints each
1046     time).
1047
1048 ``params``
1049     The ``where`` parameter described above may use standard Python database
1050     string placeholders -- ``'%s'`` to indicate parameters the database engine
1051     should automatically quote. The ``params`` argument is a list of any extra
1052     parameters to be substituted.
1053
1054     Example::
1055
1056         Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1057
1058     Always use ``params`` instead of embedding values directly into ``where``
1059     because ``params`` will ensure values are quoted correctly according to
1060     your particular backend. (For example, quotes will be escaped correctly.)
1061
1062     Bad::
1063
1064         Entry.objects.extra(where=["headline='Lennon'"])
1065
1066     Good::
1067
1068         Entry.objects.extra(where=['headline=%s'], params=['Lennon'])
1069
1070 **New in Django development version** The ``select_params`` argument to
1071 ``extra()`` is new. Previously, you could attempt to pass parameters for
1072 ``select`` in the ``params`` argument, but it worked very unreliably.
1073
1074 QuerySet methods that do not return QuerySets
1075 ---------------------------------------------
1076
1077 The following ``QuerySet`` methods evaluate the ``QuerySet`` and return
1078 something *other than* a ``QuerySet``.
1079
1080 These methods do not use a cache (see `Caching and QuerySets`_ below). Rather,
1081 they query the database each time they're called.
1082
1083 ``get(**kwargs)``
1084 ~~~~~~~~~~~~~~~~~
1085
1086 Returns the object matching the given lookup parameters, which should be in
1087 the format described in `Field lookups`_.
1088
1089 ``get()`` raises ``MultipleObjectsReturned`` if more than one object was found.
1090 The ``MultipleObjectsReturned`` exception is an attribute of the model class.
1091 For example, the following will raise ``MultipleObjectsReturned`` if there
1092 are more than one authors with the name of 'John'::
1093
1094         Author.objects.get(name='John') # raises Author.MultipleObjectsReturned
1095
1096 ``get()`` raises a ``DoesNotExist`` exception if an object wasn't found for the
1097 given parameters. The ``DoesNotExist`` exception is an attribute of the model
1098 class. Example::
1099
1100     Entry.objects.get(id='foo') # raises Entry.DoesNotExist
1101
1102 The ``DoesNotExist`` exception inherits from
1103 ``django.core.exceptions.ObjectDoesNotExist``, so you can target multiple
1104 ``DoesNotExist`` exceptions. Example::
1105
1106     from django.core.exceptions import ObjectDoesNotExist
1107     try:
1108         e = Entry.objects.get(id=3)
1109         b = Blog.objects.get(id=1)
1110     except ObjectDoesNotExist:
1111         print "Either the entry or blog doesn't exist."
1112
1113 ``create(**kwargs)``
1114 ~~~~~~~~~~~~~~~~~~~~
1115
1116 A convenience method for creating an object and saving it all in one step.  Thus::
1117
1118     p = Person.objects.create(first_name="Bruce", last_name="Springsteen")
1119
1120 and::
1121
1122     p = Person(first_name="Bruce", last_name="Springsteen")
1123     p.save()
1124
1125 are equivalent.
1126
1127 ``get_or_create(**kwargs)``
1128 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1129
1130 A convenience me