Django

Code

Changeset 2748

Show
Ignore:
Timestamp:
04/26/06 10:38:30 (3 years ago)
Author:
adrian
Message:

magic-removal: Beginning of substantial rewrite to docs/db-api.txt. Not yet finished.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/docs/db-api.txt

    r2741 r2748  
    33====================== 
    44 
    5 Once you've created your `data models`_, you'll need to retrieve data from the 
    6 database. This document explains the database abstraction API derived from th
    7 models, and how to create, retrieve and update objects
     5Once you've created your `data models`_, Django automatically gives you a 
     6database-abstraction API that lets you create, retrieve, update and delet
     7objects. This document explains that API
    88 
    99.. _`data models`: http://www.djangoproject.com/documentation/model_api/ 
    1010 
    11 Throughout this reference, we'll refer to the following Poll application:: 
    12  
    13     class Poll(models.Model): 
    14         slug = models.SlugField(unique_for_month='pub_date') 
    15         question = models.CharField(maxlength=255) 
     11Throughout this reference, we'll refer to the following Blog application:: 
     12 
     13    class Blog(models.Model): 
     14        name = models.CharField(maxlength=100) 
     15        tagline = models.TextField() 
     16 
     17        def __repr__(self): 
     18            return self.name 
     19 
     20    class Author(models.Model): 
     21        name = models.CharField(maxlength=50) 
     22        email = models.URLField() 
     23 
     24        class __repr__(self): 
     25            return self.name 
     26 
     27    class Entry(models.Model): 
     28        blog = models.ForeignKey(Blog) 
     29        headline = models.CharField(maxlength=255) 
     30        body_text = models.TextField() 
    1631        pub_date = models.DateTimeField() 
    17         expire_date = models.DateTimeField(
     32        authors = models.ManyToManyField(Author
    1833 
    1934        def __repr__(self): 
    20             return self.question 
    21  
    22         class Meta: 
    23             get_latest_by = 'pub_date' 
    24  
    25     class Choice(models.Model): 
    26         poll = models.ForeignKey(Poll, edit_inline=meta.TABULAR, 
    27             num_in_admin=10, min_num_in_admin=5) 
    28         choice = models.CharField(maxlength=255, core=True) 
    29         votes = models.IntegerField(editable=False, default=0) 
    30  
    31         def __repr__(self): 
    32             return self.choice 
    33  
    34 and the following Django sample session:: 
    35  
    36     >>> from datetime import datetime 
    37     >>> p1 = Poll(slug='whatsup', question="What's up?", 
    38     ...     pub_date=datetime(2005, 2, 20), expire_date=datetime(2005, 4, 20)) 
    39     >>> p1.save() 
    40     >>> p2 = Poll(slug='name', question="What's your name?", 
    41     ...     pub_date=datetime(2005, 3, 20), expire_date=datetime(2005, 3, 25)) 
    42     >>> p2.save() 
    43     >>> Poll.objects.all() 
    44     [What's up?, What's your name?] 
    45  
    46 How queries work 
     35            return self.headline 
     36 
     37Creating objects 
    4738================ 
    4839 
    49 Querying in Django is based upon the construction and evaluation of Query 
    50 Sets. 
    51  
    52 A Query Set is a database-independent representation of a group of objects 
    53 that all meet a given set of criteria. However, the determination of which 
    54 objects are actually members of the Query Set is not made until you formally 
    55 evaluate the Query Set. 
    56  
    57 To construct a Query Set that meets your requirements, you start by obtaining 
    58 an initial Query Set that describes all objects of a given type. This initial 
    59 Query Set can then be refined using a range of operations. Once you have 
    60 refined your Query Set to the point where it describes the group of objects 
    61 you require, it can be evaluated (using iterators, slicing, or one of a range 
    62 of other techniques), yielding an object or list of objects that meet the 
    63 specifications of the Query Set. 
    64  
    65 Obtaining an initial QuerySet 
    66 ============================= 
    67  
    68 Every model has at least one Manager; by default, the Manager is called 
    69 ``objects``. One of the most important roles of the Manager is as a source 
    70 of initial Query Sets. The Manager acts as a Query Set that describes all 
    71 objects of the type being managed; ``Polls.objects`` is the initial Query Set 
    72 that contains all Polls in the database. 
    73  
    74 The initial Query Set on the Manager behaves in the same way as every other 
    75 Query Set in every respect except one - it cannot be evaluated. To overcome 
    76 this limitation, the Manager Query Set has an ``all()`` method. The ``all()`` 
    77 method produces a copy of the initial Query Set - a copy that *can* be 
    78 evaluated:: 
    79  
    80     all_polls = Poll.objects.all() 
    81  
    82 See the `Managers`_ section of the Model API for more details on the role 
    83 and construction of Managers. 
    84  
    85 .. _Managers: http://www.djangoproject.com/documentation/model_api/#managers 
    86  
    87 QuerySet refinement 
    88 =================== 
    89  
    90 The initial Query Set provided by the Manager describes all objects of a 
    91 given type. However, you will usually need to describe a subset of the 
     40To create an object, instantiate it using keyword arguments to the model class, 
     41then call ``save()`` to save it to the database. 
     42 
     43Example:: 
     44 
     45    b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') 
     46    b.save() 
     47 
     48This performs an ``INSERT`` SQL statement behind the scenes. Django doesn't hit 
     49the database until you explicitly call ``save()``. 
     50 
     51The ``save()`` method has no return value. 
     52 
     53Auto-incrementing primary keys 
     54------------------------------ 
     55 
     56If a model has an ``AutoField`` -- an auto-incrementing primary key -- then 
     57that auto-incremented value will be calculated and saved as an attribute on 
     58your object the first time you call ``save()``. 
     59 
     60Example:: 
     61 
     62    b2 = Blog(name='Cheddar Talk', tagline='Thoughts on cheese.') 
     63    b2.id     # Returns None, because b doesn't have an ID yet. 
     64    b2.save() 
     65    b2.id     # Returns the ID of your new object. 
     66 
     67There's no way to tell what the value of an ID will be before you call 
     68``save()``, because that value is calculated by your database, not by Django. 
     69 
     70(For convenience, each model has an ``AutoField`` named ``id`` by default 
     71unless you explicitly specify ``primary_key=True`` on a field. See the 
     72`AutoField documentation`_.) 
     73 
     74.. _AutoField documentation: TODO: Link 
     75 
     76Explicitly specifying auto-primary-key values 
     77~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     78 
     79If a model has an ``AutoField`` but you want to define a new object's ID 
     80explicitly when saving, just define it explicitly before saving, rather than 
     81relying on the auto-assignment of the ID. 
     82 
     83Example:: 
     84 
     85    b3 = Blog(id=3, name='Cheddar Talk', tagline='Thoughts on cheese.') 
     86    b3.id     # Returns 3. 
     87    b3.save() 
     88    b3.id     # Returns 3. 
     89 
     90If you assign auto-primary-key values manually, make sure not to use an 
     91already-existing primary-key value! If you create a new object with an explicit 
     92primary-key value that already exists in the database, Django will assume 
     93you're changing the existing record rather than creating a new one. 
     94 
     95Given the above ``'Cheddar Talk'`` blog example, this example would override 
     96the previous record in the database:: 
     97 
     98    b4 = Blog(id=3, name='Not Cheddar', tagline='Anything but cheese.') 
     99    b4.save()  # Overrides the previous blog with ID=3! 
     100 
     101See also "How Django knows to UPDATE vs. INSERT", below. 
     102 
     103Explicitly specifying auto-primary-key values is mostly useful for bulk-saving 
     104objects, when you're confident you won't have primary-key collision. 
     105 
     106Saving changes to objects 
     107========================= 
     108 
     109To save changes to an object that's already in the database, use ``save()``. 
     110 
     111Given a ``Blog`` instance ``b5`` that has already been saved to the database, 
     112this example changes its name and updates its record in the database:: 
     113 
     114    b5.name = 'New name' 
     115    b5.save() 
     116 
     117This performs an ``UPDATE`` SQL statement behind the scenes. Django doesn't hit 
     118the database until you explicitly call ``save()``. 
     119 
     120The ``save()`` method has no return value. 
     121 
     122How Django knows to UPDATE vs. INSERT 
     123------------------------------------- 
     124 
     125You may have noticed Django database objects use the same ``save()`` method 
     126for creating and changing objects. Django abstracts the need to use ``INSERT`` 
     127or ``UPDATE`` SQL statements. Specifically, when you call ``save()``, Django 
     128follows this algorithm: 
     129 
     130    * If the object's primary key attribute is set, Django executes a 
     131      ``SELECT`` query to determine whether a record with the given primary key 
     132      already exists. 
     133    * If the record with the given primary key does already exist, Django 
     134      executes an ``UPDATE`` query. 
     135    * If the object's primary key attribute is *not* set, or if it's set but a 
     136      record doesn't exist, Django executes an ``INSERT``. 
     137 
     138The one gotcha here is that you should be careful not to specify a primary-key 
     139value explicitly when saving new objects, if you cannot guarantee the 
     140primary-key value is unused. For more on this nuance, see 
     141"Explicitly specifying auto-primary-key values" above. 
     142 
     143Retrieving objects 
     144================== 
     145 
     146To retrieve objects from your database, you construct a ``QuerySet`` via a 
     147``Manager``. 
     148 
     149A ``QuerySet`` represents a collection of objects from your database. It can 
     150have zero, one or many *filters* -- criteria that narrow down the collection 
     151based on given parameters. 
     152 
     153In SQL terms, a ``QuerySet`` equates to a ``SELECT`` statement, and a filter is 
     154a limiting clause such as ``WHERE`` or ``LIMIT``. 
     155 
     156You get a ``QuerySet`` by using your model's ``Manager``. Each model has at 
     157least one ``Manager``, and it's called ``objects`` by default. Access it 
     158directly via the model class, like so:: 
     159 
     160    Blog.objects  # <django.db.models.manager.Manager object at ...> 
     161    b = Blog(name='Foo', tagline='Bar') 
     162    b.objects     # AttributeError: "Manager isn't accessible via Blog instances." 
     163 
     164(``Managers`` are accessible only via model classes, rather than from model 
     165instances, to enforce a separation between "table-level" operations and 
     166"record-level" operations.) 
     167 
     168The ``Manager`` is the main source of ``QuerySets`` for a model. It acts as a 
     169"root" ``QuerySet`` that describes all objects in the model's database table. 
     170For example, ``Blog.objects`` is the initial ``QuerySet`` that contains all 
     171``Blog`` objects in the database. 
     172 
     173Retrieving all objects 
     174---------------------- 
     175 
     176The simplest way to retrieve objects from a table is to get all of them. 
     177To do this, use the ``all()`` method on a ``Manager``. 
     178 
     179Example:: 
     180 
     181    all_entries = Entry.objects.all() 
     182 
     183The ``all()`` method returns a ``QuerySet`` of all the objects in the database. 
     184 
     185(If ``Entry.objects`` is a ``QuerySet``, why can't we just do ``Entry.objects``? 
     186That's because ``Entry.objects``, the root ``QuerySet``, is a special case 
     187that cannot be evaluated. The ``all()`` method returns a ``QuerySet`` that 
     188*can* be evaluated.) 
     189 
     190Filtering objects 
     191----------------- 
     192 
     193The root ``QuerySet`` provided by the ``Manager`` describes all objects in the 
     194database table. Usually, though, you'll need to select only a subset of the 
    92195complete set of objects. 
    93196 
    94 To create such a subset, you refine the initial Query Set, adding conditions 
    95 until you have described a set that meets your needs. The two most common 
    96 mechanisms for refining a Query Set are: 
     197To create such a subset, you refine the initial ``QuerySet``, adding filter 
     198conditions. The two most common ways to refine a ``QuerySet`` are: 
    97199 
    98200``filter(**kwargs)`` 
    99     Returns a new Query Set containing objects that match the given lookup parameters. 
     201    Returns a new ``QuerySet`` containing objects that match the given lookup 
     202    parameters. 
    100203 
    101204``exclude(**kwargs)`` 
    102     Return a new Query Set containing objects that do not match the given lookup parameters. 
    103  
    104 Lookup parameters should be in the format described in "Field lookups" below. 
    105  
    106 The result of refining a Query Set is itself a Query Set; so it is possible to 
    107 chain refinements together. For example:: 
    108  
    109     Poll.objects.filter( 
    110         question__startswith="What").exclude( 
     205    Returns a new ``QuerySet`` containing objects that do *not* match the given 
     206    lookup parameters. 
     207 
     208The lookup parameters (``**kwargs`` in the above function definitions) should 
     209be in the format described in "Field lookups" below. 
     210 
     211For example, to get a ``QuerySet`` of blog entries from the year 2006, use 
     212``filter()`` like so:: 
     213 
     214    Entry.objects.filter(pub_date__year=2006) 
     215 
     216(Note we don't have to add an ``all()`` -- ``Entry.objects.all().filter(...)``. 
     217That would still work, but you only need ``all()`` when you want all objects 
     218from the root ``QuerySet``.) 
     219 
     220Chaining filters 
     221~~~~~~~~~~~~~~~~ 
     222 
     223The result of refining a ``QuerySet`` is itself a ``Query Set``, so it's 
     224possible to chain refinements together. For example:: 
     225 
     226    Entry.objects.filter( 
     227        headline__startswith='What').exclude( 
    111228            pub_date__gte=datetime.now()).filter( 
    112                 pub_date__gte=datetime(2005,1,1)) 
    113  
    114 ...takes the initial Query Set, and adds a filter, then an exclusion, then 
    115 another filter to remove elements present in the initial Query Set. The 
    116 final result is a Query Set containing all Polls with a question that 
    117 starts with "What", that were published between 1 Jan 2005 and today. 
    118  
    119 Each Query Set is a unique object. The process of refinement is not one 
    120 of adding a condition to the initial Query Set. Rather, each refinement 
    121 creates a separate and distinct Query Set that can be stored, used. and 
    122 reused. For example:: 
    123  
    124     q1 = Poll.objects.filter(question__startswith="What") 
     229                pub_date__gte=datetime(2005, 1, 1)) 
     230 
     231...takes the initial ``QuerySet`` of all entries in the database, adds a 
     232filter, then an exclusion, then another filter. The final result is a 
     233``QuerySet`` containing all entries with a headline that starts with "What", 
     234that were published between January 1, 2005, and the current day. 
     235 
     236Filtered QuerySets are unique 
     237----------------------------- 
     238 
     239Each time you refine a ``QuerySet``, you get a brand-new ``QuerySet`` that is 
     240in no way bound to the previous ``QuerySet``. Each refinement creates a 
     241separate and distinct ``QuerySet`` that can be stored, used and reused. 
     242 
     243Example:: 
     244 
     245    q1 = Entry.objects.filter(headline__startswith="What") 
    125246    q2 = q1.exclude(pub_date__gte=datetime.now()) 
    126247    q3 = q1.filter(pub_date__gte=datetime.now()) 
    127248 
    128 will construct 3 Query Sets; a base query set containing all Polls with a 
    129 question that starts with "What", and two subsets of the base Query Set (one 
    130 with an exlusion, one with a filter). The initial Query Set is unaffected by 
    131 the refinement process. 
    132  
    133 It should be noted that the construction of a Query Set does not involve any 
    134 activity on the database. The database is not consulted until a Query Set is 
    135 evaluated. 
     249These three ``QuerySets`` are separate. The first is a base ``QuerySet`` 
     250containing all entries that contain a headline starting with "What". The second 
     251is a subset of the first, with an additional criteria that excludes records 
     252whose ``pub_date`` is greater than now. The third is a subset of the first, 
     253with an additional criteria that selects only the records whose ``pub_date`` is 
     254greater than now. The initial ``QuerySet`` (``q1``) is unaffected by the 
     255refinement process. 
     256 
     257QuerySets are lazy 
     258------------------ 
     259 
     260``QuerySets`` are lazy -- the act of creating a ``QuerySet`` doesn't involve 
     261any database activity. You can stack filters together all day long, and Django 
     262won't actually run the query until the ``QuerySet`` is *evaluated*. 
     263 
     264When QuerySets are evaluated 
     265~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     266 
     267You can evaluate a ``QuerySet`` in the following ways: 
     268 
     269    * **Iteration.** A ``QuerySet`` is iterable, and it executes its database 
     270      query the first time you iterate over it. For example, this will print 
     271      the headline of all entries in the database:: 
     272 
     273          for e in Entry.objects.all(): 
     274              print e.headline 
     275 
     276    * **Slicing.** A ``QuerySet`` can be sliced, using Python's array-slicing 
     277      syntax, and it executes its database query the first time you slice it. 
     278      Examples:: 
     279 
     280          fifth_entry = Entry.objects.all()[4] 
     281          all_entries_but_the_first_two = Entry.objects.all()[2:] 
     282          every_second_entry = Entry.objects.all()[::2] 
     283 
     284    * **repr().** A ``QuerySet`` is evaluated when you call ``repr()`` on it. 
     285      This is for convenience in the Python interactive interpreter, so you can 
     286      immediately see your results. 
     287 
     288    * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it. 
     289      This, as you might expect, returns the length of the result list. 
     290 
     291      Note: *Don't* use ``len()`` on ``QuerySet``s if all you want to do is 
     292      determine the number of records in the set. It's much more efficient to 
     293      handle a count at the database level, using SQL's ``SELECT COUNT(*)``, 
     294      and Django provides a ``count()`` method for precisely this reason. See 
     295      ``count()`` below. 
     296 
     297    * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on 
     298      it. For example:: 
     299 
     300          entry_list = list(Entry.objects.all()) 
     301 
     302      Be warned, though, that this could have a large memory overhead, because 
     303      Django will load each element of the list into memory. In contrast, 
     304      iterating over a ``QuerySet`` will take advantage of your database to 
     305      load data and instantiate objects only as you need them. 
     306 
     307Full list of QuerySet methods 
     308----------------------------- 
     309 
     310Django provides a range of ``QuerySet`` refinement methods that modify either 
     311the types of results returned by the ``QuerySet`` or the way its SQL query is 
     312executed. 
     313 
     314filter(**kwargs) 
     315~~~~~~~~~~~~~~~~ 
     316 
     317Returns a new ``QuerySet`` containing objects that match the given lookup 
     318parameters. 
     319 
     320The lookup parameters (``**kwargs``) should be in the format described in 
     321"Field lookups" below. Multiple parameters are joined via ``AND`` in the 
     322underlying SQL statement. 
     323 
     324exclude(**kwargs) 
     325~~~~~~~~~~~~~~~~~ 
     326 
     327Returns a new ``QuerySet`` containing objects that do *not* match the given 
     328lookup parameters. 
     329 
     330The lookup parameters (``**kwargs``) should be in the format described in 
     331"Field lookups" below. Multiple parameters are joined via ``AND`` in the 
     332underlying SQL statement, and the whole thing is enclosed in a ``NOT()``. 
     333 
     334This example excludes all entries whose ``pub_date`` is the current date/time 
     335AND whose ``headline`` is "Hello":: 
     336 
     337    Entry.objects.exclude(pub_date__gt=datetime.now(), headline='Hello') 
     338 
     339This example excludes all entries whose ``pub_date`` is the current date/time 
     340OR whose ``headline`` is "Hello":: 
     341 
     342    Entry.objects.exclude(pub_date__gt=datetime.now()).exclude(headline='Hello') 
     343 
     344Note the second example is more restrictive. 
     345 
     346order_by(*fields) 
     347~~~~~~~~~~~~~~~~~ 
     348 
     349By default, results returned by a ``QuerySet`` are ordered by the ordering 
     350tuple given by the ``ordering`` option in the model's ``Meta``. You can 
     351override this on a per-``QuerySet`` basis by using the ``order_by`` method. 
     352 
     353Example:: 
     354 
     355    Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline') 
     356 
     357The result above will be ordered by ``pub_date`` descending, then by 
     358``headline`` ascending. The negative sign in front of ``"-pub_date"`` indicates 
     359*descending* order. Ascending order is implied. To order randomly, use ``"?"``, 
     360like so:: 
     361 
     362    Entry.objects.order_by('?') 
     363 
     364To order by a field in a different table, add the other table's name and a dot, 
     365like so:: 
     366 
     367    Entry.objects.order_by('blogs_blog.name', 'headline') 
     368 
     369There's no way to specify whether ordering should be case sensitive. With 
     370respect to case-sensitivity, Django will order results however your database 
     371backend normally orders them. 
     372 
     373values(*fields) 
     374--------------- 
     375 
     376Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of 
     377dictionaries instead of model-instance objects. 
     378 
     379Each of those dictionaries represents an object, with the keys corresponding to 
     380the attribute names of model objects. 
     381 
     382This example compares the dictionaries of ``values()`` with the normal model 
     383objects:: 
     384 
     385    # This list contains a Blog object. 
     386    >>> Blog.objects.filter(name__startswith='Beatles') 
     387    [Beatles Blog] 
     388 
     389    # This list contains a dictionary. 
     390    >>> Blog.objects.filter(name__startswith='Beatles').values() 
     391    [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}] 
     392 
     393``values()`` takes optional positional arguments, ``*fields``, which specify 
     394field names to which the ``SELECT`` should be limited. If you specify the 
     395fields, each dictionary will contain only the field keys/values for the fields 
     396you specify. If you don't specify the fields, each dictionary will contain a 
     397key and value for every field in the database table. 
     398 
     399Example:: 
     400 
     401    >>> Blog.objects.values() 
     402    [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}], 
     403    >>> Blog.objects.values('id', 'name') 
     404    [{'id': 1, 'name': 'Beatles Blog'}] 
     405 
     406A ``ValuesQuerySet`` is useful when you know you're only going to need values 
     407from a small number of the available fields and you won't need the 
     408functionality of a model instance object. It's more efficient to select only 
     409the fields you need to use. 
     410 
     411Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all 
     412methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or 
     413whatever. Yes, that means these two calls are identical:: 
     414 
     415    Blog.objects.values().order_by('id') 
     416    Blog.objects.order_by('id').values() 
     417 
     418The people who made Django prefer to put all the SQL-affecting methods first, 
     419followed (optionally) by any output-affecting methods (such as ``values()``), 
     420but it doesn't really matter. This is your chance to really flaunt your 
     421individualism. 
     422 
     423distinct() 
     424~~~~~~~~~~ 
     425 
     426The ``distinct()`` method returns a new ``QuerySet`` that uses 
     427``SELECT DISTINCT`` in its SQL query. This eliminates duplicate rows from the 
     428query results. 
     429 
     430By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this 
     431is rarely a problem, because simple queries such as ``Blog.objects.all()`` 
     432don't introduce the possibility of duplicate result rows. 
     433 
     434However, if your query spans multiple tables, or you're using a 
     435``ValuesQuerySet`` with a ``fields`` clause, it's possible to get duplicate 
     436results when a ``QuerySet`` is evaluated. That's when you'd use ``distinct()``. 
     437 
     438TODO: Left off here 
     439 
     440``dates(field, kind, order='ASC')`` 
     441----------------------------------- 
     442 
     443Returns a Date Query Set - a Query Set that evaluates to a list of 
     444``datetime.datetime`` objects representing all available dates of a 
     445particular kind within the contents of the Query Set. 
     446 
     447``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your 
     448model. 
     449 
     450``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each 
     451``datetime.datetime`` object in the result list is "truncated" to the given 
     452``type``. 
     453 
     454    * ``"year"`` returns a list of all distinct year values for the field. 
     455    * ``"month"`` returns a list of all distinct year/month values for the field. 
     456    * ``"day"`` returns a list of all distinct year/month/day values for the field. 
     457 
     458``order``, which defaults to ``'ASC'``, should be either ``"ASC"`` or ``"DESC"``. 
     459This specifies how to order the results. 
     460 
     461For example:: 
     462 
     463    >>> Poll.objects.dates('pub_date', 'year') 
     464    [datetime.datetime(2005, 1, 1)] 
     465    >>> Poll.objects.dates('pub_date', 'month') 
     466    [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)] 
     467    >>> Poll.objects.dates('pub_date', 'day') 
     468    [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)] 
     469    >>> Poll.objects.dates('pub_date', 'day', order='DESC') 
     470    [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)] 
     471    >>> Poll.objects.filter(question__contains='name').dates('pub_date', 'day') 
     472    [datetime.datetime(2005, 3, 20)] 
     473 
     474``select_related()`` 
     475-------------------- 
     476 
     477Relations are the bread and butter of databases, so there's an option to "follow" 
     478all relationships and pre-fill them in a simple cache so that later calls to 
     479objects with a one-to-many relationship don't have to hit the database. Do this by 
     480passing ``select_related=True`` to a lookup. This results in (sometimes much) larger 
     481queries, but it means that later use of relationships is much faster. 
     482 
     483For example, using the Poll and Choice models from above, if you do the following:: 
     484 
     485    c = Choice.objects.select_related().get(id=5) 
     486 
     487Then subsequent calls to ``c.poll`` won't hit the database. 
     488 
     489Note that ``select_related`` follows foreign keys as far as possible. If you have the 
     490following models:: 
     491 
     492    class Poll(models.Model): 
     493        # ... 
     494 
     495    class Choice(models.Model): 
     496        # ... 
     497        poll = models.ForeignKey(Poll) 
     498 
     499    class SingleVote(meta.Model): 
     500        # ... 
     501        choice = models.ForeignKey(Choice) 
     502 
     503then a call to ``SingleVotes.objects.select_related().get(id=4)`` will 
     504cache the related choice *and* the related poll:: 
     505 
     506    >>> sv = SingleVotes.objects.select_related().get(id=4) 
     507    >>> c = sv.choice        # Doesn't hit the database. 
     508    >>> p = c.poll           # Doesn't hit the database. 
     509 
     510    >>> sv = SingleVotes.objects.get(id=4) 
     511    >>> c = sv.choice        # Hits the database. 
     512    >>> p = c.poll           # Hits the database. 
     513 
     514 
     515``extra(params, select, where, tables)`` 
     516---------------------------------------- 
     517 
     518Sometimes, the Django query syntax by itself isn't quite enough. To cater for these 
     519edge cases, Django provides the ``extra()`` Query Set modifier - a mechanism 
     520for injecting specific clauses into the SQL generated by a Query Set. 
     521 
     522Note that by definition these extra lookups may not be portable to different 
     523database engines (because you're explicitly writing SQL code) and should be 
     524avoided if possible.: 
     525 
     526``params`` 
     527    All the extra-SQL params described below may use standard Python string 
     528    formatting codes to indicate parameters that the database engine will 
     529    automatically quote.  The ``params`` argument can contain any extra 
     530    parameters to be substituted. 
     531 
     532``select`` 
     533    The ``select`` keyword allows you to select extra fields.  This should be a 
     534    dictionary mapping attribute names to a SQL clause to use to calculate that 
     535    attribute. For example:: 
     536 
     537        Poll.objects.extra( 
     538            select={ 
     539                'choice_count': 'SELECT COUNT(*) FROM choices WHERE poll_id = polls.id' 
     540            } 
     541        ) 
     542 
     543    Each of the resulting ``Poll`` objects will have an extra attribute, ``choice_count``, 
     544    an integer count of associated ``Choice`` objects. Note that the parenthesis required by 
     545    most database engines around sub-selects are not required in Django's ``select`` 
     546    clauses. 
     547 
     548``where`` / ``tables`` 
     549    If you need to explicitly pass extra ``WHERE`` clauses -- perhaps to perform 
     550    non-explicit joins -- use the ``where`` keyword. If you need to 
     551    join other tables into your query, you can pass their names to ``tables``. 
     552 
     553    ``where`` and ``tables`` both take a list of strings. All ``where`` parameters 
     554    are "AND"ed to any other search criteria. 
     555 
     556    For example:: 
     557 
     558        Poll.objects.filter( 
     559            question__startswith='Who').extra(where=['id IN (3, 4, 5, 20)']) 
     560 
     561    ...translates (roughly) into the following SQL:: 
     562 
     563        SELECT * FROM polls_polls WHERE question LIKE 'Who%' AND id IN (3, 4, 5, 20); 
     564 
     565 
     566 
     567Caching and QuerySets 
     568--------------------- 
     569 
     570Each ``QuerySet`` contains a cache, to minimize database access. 
     571 
     572In a newly created ``QuerySet``, this cache is empty. The first time a 
     573``QuerySet`` is evaluated -- and, hence, a database query happens -- Django 
     574saves the query results in the ``QuerySet``'s cache and returns the results 
     575that have been explicitly requested (e.g., the next element, if the 
     576``QuerySet`` is being iterated over). Subsequent evaluations of the 
     577``QuerySet`` reuse the cached results. 
     578 
     579Keep this caching behavior in mind, because it may bite you if you don't use 
     580your ``QuerySet``s correctly. For example, the following will create two 
     581``QuerySet``s, evaluate them, and throw them away:: 
     582 
     583    print [e.headline for e in Entry.objects.all()] 
     584    print [e.pub_date for e in Entry.objects.all()] 
     585 
     586That means the same database query will be executed twice, effectively doubling 
     587your database load. Also, there's a possibility the two lists may not include 
     588the same database records, because an ``Entry`` may have been added or deleted 
     589in the split second between the two requests. 
     590 
     591To avoid this problem, simply save the ``QuerySet`` and reuse it:: 
     592 
     593    queryset = Poll.objects.all() 
     594    print [p.headline for p in queryset] # Evaluate the query set. 
     595    print [p.pub_date for p in queryset] # Re-use the cache from the evaluation. 
     596 
     597 
     598 
     599Deleting objects 
     600================ 
     601 
     602 
     603 
    136604 
    137605Field lookups 
     
    304772.. _OR lookups examples page: http://www.djangoproject.com/documentation/models/or_lookups/ 
    305773 
    306 QuerySet evaluation 
    307 =================== 
    308  
    309 A Query Set must be evaluated to return the objects that are contained in the 
    310 set. This can be achieved by iteration, slicing, or by specialist function. 
    311  
    312 A Query Set is an iterable object. Therefore, it can be used in loop 
    313 constructs. For example:: 
    314  
    315     for p in Poll.objects.all(): 
    316         print p 
    317  
    318 will print all the Poll objects, using the ``__repr__()`` method of Poll. 
    319  
    320 A Query Set can also be sliced, using array notation:: 
    321  
    322     fifth_poll = Poll.objects.all()[4] 
    323     all_polls_but_the_first_two = Poll.objects.all()[2:] 
    324     every_second_poll = Poll.objects.all()[::2] 
    325  
    326 Query Sets are lazy objects - that is, they are not *actually* sets (or 
    327 lists) that contain all the objects that they represent. Python protocol 
    328 magic is used to make the Query Set *look* like an iterable, sliceable 
    329 object, but behind the scenes, Django is using caching to only instantiate 
    330 objects as they are required. 
    331  
    332 If you really need to have a list, you can force the evaluation of the 
    333 lazy object:: 
    334  
    335     querylist = list(Poll.objects.all()) 
    336  
    337 However - be warned; this could have a large memory overhead, as Django will 
    338 create an in-memory representation of every element of the list. 
    339  
    340 Caching and QuerySets 
    341 ===================== 
    342  
    343 Each Query Set contains a cache. In a newly created Query Set, this cache 
    344 is unpopulated. When a Query Set is evaluated for the first time, Django 
    345 makes a database query to populate the cache, and then returns the results 
    346 that have been explicitly requested (e.g., the next element if iteration 
    347 is in use). Subsequent evaluations of the Query Set reuse the cached results. 
    348  
    349 This caching behavior must be kept in mind when using Query Sets. For 
    350 example, the following will cause two temporary Query Sets to be created, 
    351 evaluated, and thrown away:: 
    352  
    353     print [p for p in Poll.objects.all()] # Evaluate the Query Set 
    354     print [p for p in Poll.objects.all()] # Evaluate the Query Set again 
    355  
    356 On a small, low-traffic website, this may not pose a serious problem. However, 
    357 on a high traffic website, it effectively doubles your database load. In 
    358 addition, there is a possibility that the two lists may not be identical, 
    359 since a poll may be added or deleted by another user between making the two 
    360 requests. 
    361  
    362 To avoid this problem, simply save the Query Set and reuse it:: 
    363  
    364     queryset = Poll.objects.all() 
    365     print [p for p in queryset] # Evaluate the query set 
    366     print [p for p in queryset] # Re-use the cache from the evaluation 
    367774 
    368775Specialist QuerySet evaluation 
     
    564971and conditions required for the SQL query. 
    565972 
    566 Specialist QuerySets refinement 
    567 =============================== 
    568  
    569 In addition to ``filter`` and ``exclude()``, Django provides a range of 
    570 Query Set refinement methods that modify the types of results returned by 
    571 the Query Set, or modify the way the SQL query is executed on the database. 
    572  
    573 ``order_by(*fields)`` 
    574 ---------------------- 
    575  
    576 The results returned by a Query Set are automatically ordered by the ordering 
    577 tuple given by the ``ordering`` meta key in the model. However, ordering may be 
    578 explicitly provided by using the ``order_by`` method:: 
    579  
    580     Poll.objects.filter(pub_date__year=2005, 
    581         pub_date__month=1).order_by('-pub_date', 'question') 
    582  
    583 The result set above will be ordered by ``pub_date`` descending, then 
    584 by ``question`` ascending. The negative sign in front of "-pub_date" indicates 
    585 descending order. Ascending order is implied. To order randomly, use "?", like 
    586 so:: 
    587  
    588     Poll.objects.order_by=('?') 
    589  
    590 To order by a field in a different table, add the other table's name and a dot, 
    591 like so:: 
    592  
    593     Choice.objects.order_by=('Poll.pub_date', 'choice') 
    594  
    595 There's no way to specify whether ordering should be case sensitive. With 
    596 respect to case-sensitivity, Django will order results however your database 
    597 backend normally orders them. 
    598  
    599 ``distinct()`` 
    600 -------------- 
    601  
    602 By default, a Query Set will not eliminate duplicate rows. This will not 
    603 happen during simple queries; however, if your query spans relations, 
    604 or you are using a Values Query Set with a ``fields`` clause, it is possible 
    605 to get duplicated results when a Query Set is evaluated. 
    606  
    607 ``distinct()`` returns a new Query Set that eliminates duplicate rows from the 
    608 results returned by the Query Set. This is equivalent to a ``SELECT DISTINCT`` 
    609 SQL clause. 
    610  
    611 ``values(*fields)`` 
    612 -------------------- 
    613  
    614 Returns a Values Query Set - a Query Set that evaluates to a list of 
    615 dictionaries instead of model-instance objects. Each dictionary in the 
    616 list will represent an object matching the query, with the keys matching 
    617 the attribute names of the object. 
    618  
    619 It accepts an optional parameter, ``fields``, which should be a list or tuple 
    620 of field names. If you don't specify ``fields``, each dictionary in the list 
    621 returned by ``get_values()`` will have a key and value for each field in the 
    622 database table. If you specify ``fields``, each dictionary will have only the 
    623 field keys/values for the fields you specify. For example:: 
    624  
    625     >>> Poll.objects.values() 
    626     [{'id': 1, 'slug': 'whatsup', 'question': "What's up?", 
    627             'pub_date': datetime.datetime(2005, 2, 20), 
    628             'expire_date': datetime.datetime(2005, 3, 20)}, 
    629      {'id': 2, 'slug': 'name', 'question': "What's your name?", 
    630             'pub_date': datetime.datetime(2005, 3, 20), 
    631             'expire_date': datetime.datetime(2005, 4, 20)}] 
    632     >>> Poll.objects.values('id', 'slug') 
    633     [{'id': 1, 'slug': 'whatsup'}, {'id': 2, 'slug': 'name'}] 
    634  
    635 A Values Query Set is useful when you know you're only going to need values 
    636 from a small number of the available fields and you won't need the 
    637 functionality of a model instance object. It's more efficient to select only 
    638 the fields you need to use. 
    639  
    640 ``dates(field, kind, order='ASC')`` 
    641 ----------------------------------- 
    642  
    643 Returns a Date Query Set - a Query Set that evaluates to a list of 
    644 ``datetime.datetime`` objects representing all available dates of a 
    645 particular kind within the contents of the Query Set. 
    646  
    647 ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your 
    648 model. 
    649  
    650 ``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each 
    651 ``datetime.datetime`` object in the result list is "truncated" to the given 
    652 ``type``. 
    653  
    654     * ``"year"`` returns a list of all distinct year values for the field. 
    655     * ``"month"`` returns a list of all distinct year/month values for the field. 
    656     * ``"day"`` returns a list of all distinct year/month/day values for the field. 
    657  
    658 ``order``, which defaults to ``'ASC'``, should be either ``"ASC"`` or ``"DESC"``. 
    659 This specifies how to order the results. 
    660  
    661 For example:: 
    662  
    663     >>> Poll.objects.dates('pub_date', 'year') 
    664     [datetime.datetime(2005, 1, 1)] 
    665     >>> Poll.objects.dates('pub_date', 'month') 
    666     [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)] 
    667     >>> Poll.objects.dates('pub_date', 'day') 
    668     [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)] 
    669     >>> Poll.objects.dates('pub_date', 'day', order='DESC') 
    670     [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)] 
    671     >>> Poll.objects.filter(question__contains='name').dates('pub_date', 'day') 
    672     [datetime.datetime(2005, 3, 20)] 
    673  
    674 ``select_related()`` 
    675 -------------------- 
    676  
    677 Relations are the bread and butter of databases, so there's an option to "follow" 
    678 all relationships and pre-fill them in a simple cache so that later calls to 
    679 objects with a one-to-many relationship don't have to hit the database. Do this by 
    680 passing ``select_related=True`` to a lookup. This results in (sometimes much) larger 
    681 queries, but it means that later use of relationships is much faster. 
    682  
    683 For example, using the Poll and Choice models from above, if you do the following:: 
    684  
    685     c = Choice.objects.select_related().get(id=5) 
    686  
    687 Then subsequent calls to ``c.poll`` won't hit the database. 
    688  
    689 Note that ``select_related`` follows foreign keys as far as possible. If you have the 
    690 following models:: 
    691  
    692     class Poll(models.Model): 
    693         # ... 
    694  
    695     class Choice(models.Model): 
    696         # ... 
    697         poll = models.ForeignKey(Poll) 
    698  
    699     class SingleVote(meta.Model): 
    700         # ... 
    701         choice = models.ForeignKey(Choice) 
    702  
    703 then a call to ``SingleVotes.objects.select_related().get(id=4)`` will 
    704 cache the related choice *and* the related poll:: 
    705  
    706     >>> sv = SingleVotes.objects.select_related().get(id=4) 
    707     >>> c = sv.choice        # Doesn't hit the database. 
    708     >>> p = c.poll           # Doesn't hit the database. 
    709  
    710     >>> sv = SingleVotes.objects.get(id=4) 
    711     >>> c = sv.choice        # Hits the database. 
    712     >>> p = c.poll           # Hits the database. 
    713  
    714  
    715 ``extra(params, select, where, tables)`` 
    716 ---------------------------------------- 
    717  
    718 Sometimes, the Django query syntax by itself isn't quite enough. To cater for these 
    719 edge cases, Django provides the ``extra()`` Query Set modifier - a mechanism 
    720 for injecting specific clauses into the SQL generated by a Query Set. 
    721  
    722 Note that by definition these extra lookups may not be portable to different 
    723 database engines (because you're explicitly writing SQL code) and should be 
    724 avoided if possible.: 
    725  
    726 ``params`` 
    727     All the extra-SQL params described below may use standard Python string 
    728     formatting codes to indicate parameters that the database engine will 
    729     automatically quote.  The ``params`` argument can contain any extra 
    730     parameters to be substituted. 
    731  
    732 ``select`` 
    733     The ``select`` keyword allows you to select extra fields.  This should be a 
    734     dictionary mapping attribute names to a SQL clause to use to calculate that 
    735     attribute. For example:: 
    736  
    737         Poll.objects.extra( 
    738             select={ 
    739                 'choice_count': 'SELECT COUNT(*) FROM choices WHERE poll_id = polls.id' 
    740             } 
    741         ) 
    742  
    743     Each of the resulting ``Poll`` objects will have an extra attribute, ``choice_count``, 
    744     an integer count of associated ``Choice`` objects. Note that the parenthesis required by 
    745     most database engines around sub-selects are not required in Django's ``select`` 
    746     clauses. 
    747  
    748 ``where`` / ``tables`` 
    749     If you need to explicitly pass extra ``WHERE`` clauses -- perhaps to perform 
    750     non-explicit joins -- use the ``where`` keyword. If you need to 
    751     join other tables into your query, you can pass their names to ``tables``. 
    752  
    753     ``where`` and ``tables`` both take a list of strings. All ``where`` parameters 
    754     are "AND"ed to any other search criteria. 
    755  
    756     For example:: 
    757  
    758         Poll.objects.filter( 
    759             question__startswith='Who').extra(where=['id IN (3, 4, 5, 20)']) 
    760  
    761     ...translates (roughly) into the following SQL:: 
    762  
    763         SELECT * FROM polls_polls WHERE question LIKE 'Who%' AND id IN (3, 4, 5, 20); 
    764  
    765 Changing objects 
    766 ================ 
    767  
    768 Once you've retrieved an object from the database using any of the above 
    769 options, changing it is extremely easy.  Make changes directly to the 
    770 objects fields, then call the object's ``save()`` method:: 
    771  
    772     >>> p = Polls.objects.get(id__exact=15) 
    773     >>> p.slug = "new_slug" 
    774     >>> p.pub_date = datetime.datetime.now() 
    775     >>> p.save() 
    776  
    777973Creating new objects 
    778974====================