| 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 |
|---|
| | 40 | To create an object, instantiate it using keyword arguments to the model class, |
|---|
| | 41 | then call ``save()`` to save it to the database. |
|---|
| | 42 | |
|---|
| | 43 | Example:: |
|---|
| | 44 | |
|---|
| | 45 | b = Blog(name='Beatles Blog', tagline='All the latest Beatles news.') |
|---|
| | 46 | b.save() |
|---|
| | 47 | |
|---|
| | 48 | This performs an ``INSERT`` SQL statement behind the scenes. Django doesn't hit |
|---|
| | 49 | the database until you explicitly call ``save()``. |
|---|
| | 50 | |
|---|
| | 51 | The ``save()`` method has no return value. |
|---|
| | 52 | |
|---|
| | 53 | Auto-incrementing primary keys |
|---|
| | 54 | ------------------------------ |
|---|
| | 55 | |
|---|
| | 56 | If a model has an ``AutoField`` -- an auto-incrementing primary key -- then |
|---|
| | 57 | that auto-incremented value will be calculated and saved as an attribute on |
|---|
| | 58 | your object the first time you call ``save()``. |
|---|
| | 59 | |
|---|
| | 60 | Example:: |
|---|
| | 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 | |
|---|
| | 67 | There'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 |
|---|
| | 71 | unless you explicitly specify ``primary_key=True`` on a field. See the |
|---|
| | 72 | `AutoField documentation`_.) |
|---|
| | 73 | |
|---|
| | 74 | .. _AutoField documentation: TODO: Link |
|---|
| | 75 | |
|---|
| | 76 | Explicitly specifying auto-primary-key values |
|---|
| | 77 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 78 | |
|---|
| | 79 | If a model has an ``AutoField`` but you want to define a new object's ID |
|---|
| | 80 | explicitly when saving, just define it explicitly before saving, rather than |
|---|
| | 81 | relying on the auto-assignment of the ID. |
|---|
| | 82 | |
|---|
| | 83 | Example:: |
|---|
| | 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 | |
|---|
| | 90 | If you assign auto-primary-key values manually, make sure not to use an |
|---|
| | 91 | already-existing primary-key value! If you create a new object with an explicit |
|---|
| | 92 | primary-key value that already exists in the database, Django will assume |
|---|
| | 93 | you're changing the existing record rather than creating a new one. |
|---|
| | 94 | |
|---|
| | 95 | Given the above ``'Cheddar Talk'`` blog example, this example would override |
|---|
| | 96 | the 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 | |
|---|
| | 101 | See also "How Django knows to UPDATE vs. INSERT", below. |
|---|
| | 102 | |
|---|
| | 103 | Explicitly specifying auto-primary-key values is mostly useful for bulk-saving |
|---|
| | 104 | objects, when you're confident you won't have primary-key collision. |
|---|
| | 105 | |
|---|
| | 106 | Saving changes to objects |
|---|
| | 107 | ========================= |
|---|
| | 108 | |
|---|
| | 109 | To save changes to an object that's already in the database, use ``save()``. |
|---|
| | 110 | |
|---|
| | 111 | Given a ``Blog`` instance ``b5`` that has already been saved to the database, |
|---|
| | 112 | this example changes its name and updates its record in the database:: |
|---|
| | 113 | |
|---|
| | 114 | b5.name = 'New name' |
|---|
| | 115 | b5.save() |
|---|
| | 116 | |
|---|
| | 117 | This performs an ``UPDATE`` SQL statement behind the scenes. Django doesn't hit |
|---|
| | 118 | the database until you explicitly call ``save()``. |
|---|
| | 119 | |
|---|
| | 120 | The ``save()`` method has no return value. |
|---|
| | 121 | |
|---|
| | 122 | How Django knows to UPDATE vs. INSERT |
|---|
| | 123 | ------------------------------------- |
|---|
| | 124 | |
|---|
| | 125 | You may have noticed Django database objects use the same ``save()`` method |
|---|
| | 126 | for creating and changing objects. Django abstracts the need to use ``INSERT`` |
|---|
| | 127 | or ``UPDATE`` SQL statements. Specifically, when you call ``save()``, Django |
|---|
| | 128 | follows 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 | |
|---|
| | 138 | The one gotcha here is that you should be careful not to specify a primary-key |
|---|
| | 139 | value explicitly when saving new objects, if you cannot guarantee the |
|---|
| | 140 | primary-key value is unused. For more on this nuance, see |
|---|
| | 141 | "Explicitly specifying auto-primary-key values" above. |
|---|
| | 142 | |
|---|
| | 143 | Retrieving objects |
|---|
| | 144 | ================== |
|---|
| | 145 | |
|---|
| | 146 | To retrieve objects from your database, you construct a ``QuerySet`` via a |
|---|
| | 147 | ``Manager``. |
|---|
| | 148 | |
|---|
| | 149 | A ``QuerySet`` represents a collection of objects from your database. It can |
|---|
| | 150 | have zero, one or many *filters* -- criteria that narrow down the collection |
|---|
| | 151 | based on given parameters. |
|---|
| | 152 | |
|---|
| | 153 | In SQL terms, a ``QuerySet`` equates to a ``SELECT`` statement, and a filter is |
|---|
| | 154 | a limiting clause such as ``WHERE`` or ``LIMIT``. |
|---|
| | 155 | |
|---|
| | 156 | You get a ``QuerySet`` by using your model's ``Manager``. Each model has at |
|---|
| | 157 | least one ``Manager``, and it's called ``objects`` by default. Access it |
|---|
| | 158 | directly 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 |
|---|
| | 165 | instances, to enforce a separation between "table-level" operations and |
|---|
| | 166 | "record-level" operations.) |
|---|
| | 167 | |
|---|
| | 168 | The ``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. |
|---|
| | 170 | For example, ``Blog.objects`` is the initial ``QuerySet`` that contains all |
|---|
| | 171 | ``Blog`` objects in the database. |
|---|
| | 172 | |
|---|
| | 173 | Retrieving all objects |
|---|
| | 174 | ---------------------- |
|---|
| | 175 | |
|---|
| | 176 | The simplest way to retrieve objects from a table is to get all of them. |
|---|
| | 177 | To do this, use the ``all()`` method on a ``Manager``. |
|---|
| | 178 | |
|---|
| | 179 | Example:: |
|---|
| | 180 | |
|---|
| | 181 | all_entries = Entry.objects.all() |
|---|
| | 182 | |
|---|
| | 183 | The ``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``? |
|---|
| | 186 | That's because ``Entry.objects``, the root ``QuerySet``, is a special case |
|---|
| | 187 | that cannot be evaluated. The ``all()`` method returns a ``QuerySet`` that |
|---|
| | 188 | *can* be evaluated.) |
|---|
| | 189 | |
|---|
| | 190 | Filtering objects |
|---|
| | 191 | ----------------- |
|---|
| | 192 | |
|---|
| | 193 | The root ``QuerySet`` provided by the ``Manager`` describes all objects in the |
|---|
| | 194 | database table. Usually, though, you'll need to select only a subset of the |
|---|
| 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. |
|---|
| | 249 | These three ``QuerySets`` are separate. The first is a base ``QuerySet`` |
|---|
| | 250 | containing all entries that contain a headline starting with "What". The second |
|---|
| | 251 | is a subset of the first, with an additional criteria that excludes records |
|---|
| | 252 | whose ``pub_date`` is greater than now. The third is a subset of the first, |
|---|
| | 253 | with an additional criteria that selects only the records whose ``pub_date`` is |
|---|
| | 254 | greater than now. The initial ``QuerySet`` (``q1``) is unaffected by the |
|---|
| | 255 | refinement process. |
|---|
| | 256 | |
|---|
| | 257 | QuerySets are lazy |
|---|
| | 258 | ------------------ |
|---|
| | 259 | |
|---|
| | 260 | ``QuerySets`` are lazy -- the act of creating a ``QuerySet`` doesn't involve |
|---|
| | 261 | any database activity. You can stack filters together all day long, and Django |
|---|
| | 262 | won't actually run the query until the ``QuerySet`` is *evaluated*. |
|---|
| | 263 | |
|---|
| | 264 | When QuerySets are evaluated |
|---|
| | 265 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| | 266 | |
|---|
| | 267 | You 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 | |
|---|
| | 307 | Full list of QuerySet methods |
|---|
| | 308 | ----------------------------- |
|---|
| | 309 | |
|---|
| | 310 | Django provides a range of ``QuerySet`` refinement methods that modify either |
|---|
| | 311 | the types of results returned by the ``QuerySet`` or the way its SQL query is |
|---|
| | 312 | executed. |
|---|
| | 313 | |
|---|
| | 314 | filter(**kwargs) |
|---|
| | 315 | ~~~~~~~~~~~~~~~~ |
|---|
| | 316 | |
|---|
| | 317 | Returns a new ``QuerySet`` containing objects that match the given lookup |
|---|
| | 318 | parameters. |
|---|
| | 319 | |
|---|
| | 320 | The lookup parameters (``**kwargs``) should be in the format described in |
|---|
| | 321 | "Field lookups" below. Multiple parameters are joined via ``AND`` in the |
|---|
| | 322 | underlying SQL statement. |
|---|
| | 323 | |
|---|
| | 324 | exclude(**kwargs) |
|---|
| | 325 | ~~~~~~~~~~~~~~~~~ |
|---|
| | 326 | |
|---|
| | 327 | Returns a new ``QuerySet`` containing objects that do *not* match the given |
|---|
| | 328 | lookup parameters. |
|---|
| | 329 | |
|---|
| | 330 | The lookup parameters (``**kwargs``) should be in the format described in |
|---|
| | 331 | "Field lookups" below. Multiple parameters are joined via ``AND`` in the |
|---|
| | 332 | underlying SQL statement, and the whole thing is enclosed in a ``NOT()``. |
|---|
| | 333 | |
|---|
| | 334 | This example excludes all entries whose ``pub_date`` is the current date/time |
|---|
| | 335 | AND whose ``headline`` is "Hello":: |
|---|
| | 336 | |
|---|
| | 337 | Entry.objects.exclude(pub_date__gt=datetime.now(), headline='Hello') |
|---|
| | 338 | |
|---|
| | 339 | This example excludes all entries whose ``pub_date`` is the current date/time |
|---|
| | 340 | OR whose ``headline`` is "Hello":: |
|---|
| | 341 | |
|---|
| | 342 | Entry.objects.exclude(pub_date__gt=datetime.now()).exclude(headline='Hello') |
|---|
| | 343 | |
|---|
| | 344 | Note the second example is more restrictive. |
|---|
| | 345 | |
|---|
| | 346 | order_by(*fields) |
|---|
| | 347 | ~~~~~~~~~~~~~~~~~ |
|---|
| | 348 | |
|---|
| | 349 | By default, results returned by a ``QuerySet`` are ordered by the ordering |
|---|
| | 350 | tuple given by the ``ordering`` option in the model's ``Meta``. You can |
|---|
| | 351 | override this on a per-``QuerySet`` basis by using the ``order_by`` method. |
|---|
| | 352 | |
|---|
| | 353 | Example:: |
|---|
| | 354 | |
|---|
| | 355 | Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline') |
|---|
| | 356 | |
|---|
| | 357 | The 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 ``"?"``, |
|---|
| | 360 | like so:: |
|---|
| | 361 | |
|---|
| | 362 | Entry.objects.order_by('?') |
|---|
| | 363 | |
|---|
| | 364 | To order by a field in a different table, add the other table's name and a dot, |
|---|
| | 365 | like so:: |
|---|
| | 366 | |
|---|
| | 367 | Entry.objects.order_by('blogs_blog.name', 'headline') |
|---|
| | 368 | |
|---|
| | 369 | There's no way to specify whether ordering should be case sensitive. With |
|---|
| | 370 | respect to case-sensitivity, Django will order results however your database |
|---|
| | 371 | backend normally orders them. |
|---|
| | 372 | |
|---|
| | 373 | values(*fields) |
|---|
| | 374 | --------------- |
|---|
| | 375 | |
|---|
| | 376 | Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of |
|---|
| | 377 | dictionaries instead of model-instance objects. |
|---|
| | 378 | |
|---|
| | 379 | Each of those dictionaries represents an object, with the keys corresponding to |
|---|
| | 380 | the attribute names of model objects. |
|---|
| | 381 | |
|---|
| | 382 | This example compares the dictionaries of ``values()`` with the normal model |
|---|
| | 383 | objects:: |
|---|
| | 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 |
|---|
| | 394 | field names to which the ``SELECT`` should be limited. If you specify the |
|---|
| | 395 | fields, each dictionary will contain only the field keys/values for the fields |
|---|
| | 396 | you specify. If you don't specify the fields, each dictionary will contain a |
|---|
| | 397 | key and value for every field in the database table. |
|---|
| | 398 | |
|---|
| | 399 | Example:: |
|---|
| | 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 | |
|---|
| | 406 | A ``ValuesQuerySet`` is useful when you know you're only going to need values |
|---|
| | 407 | from a small number of the available fields and you won't need the |
|---|
| | 408 | functionality of a model instance object. It's more efficient to select only |
|---|
| | 409 | the fields you need to use. |
|---|
| | 410 | |
|---|
| | 411 | Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all |
|---|
| | 412 | methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or |
|---|
| | 413 | whatever. Yes, that means these two calls are identical:: |
|---|
| | 414 | |
|---|
| | 415 | Blog.objects.values().order_by('id') |
|---|
| | 416 | Blog.objects.order_by('id').values() |
|---|
| | 417 | |
|---|
| | 418 | The people who made Django prefer to put all the SQL-affecting methods first, |
|---|
| | 419 | followed (optionally) by any output-affecting methods (such as ``values()``), |
|---|
| | 420 | but it doesn't really matter. This is your chance to really flaunt your |
|---|
| | 421 | individualism. |
|---|
| | 422 | |
|---|
| | 423 | distinct() |
|---|
| | 424 | ~~~~~~~~~~ |
|---|
| | 425 | |
|---|
| | 426 | The ``distinct()`` method returns a new ``QuerySet`` that uses |
|---|
| | 427 | ``SELECT DISTINCT`` in its SQL query. This eliminates duplicate rows from the |
|---|
| | 428 | query results. |
|---|
| | 429 | |
|---|
| | 430 | By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this |
|---|
| | 431 | is rarely a problem, because simple queries such as ``Blog.objects.all()`` |
|---|
| | 432 | don't introduce the possibility of duplicate result rows. |
|---|
| | 433 | |
|---|
| | 434 | However, if your query spans multiple tables, or you're using a |
|---|
| | 435 | ``ValuesQuerySet`` with a ``fields`` clause, it's possible to get duplicate |
|---|
| | 436 | results when a ``QuerySet`` is evaluated. That's when you'd use ``distinct()``. |
|---|
| | 437 | |
|---|
| | 438 | TODO: Left off here |
|---|
| | 439 | |
|---|
| | 440 | ``dates(field, kind, order='ASC')`` |
|---|
| | 441 | ----------------------------------- |
|---|
| | 442 | |
|---|
| | 443 | Returns a Date Query Set - a Query Set that evaluates to a list of |
|---|
| | 444 | ``datetime.datetime`` objects representing all available dates of a |
|---|
| | 445 | particular kind within the contents of the Query Set. |
|---|
| | 446 | |
|---|
| | 447 | ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your |
|---|
| | 448 | model. |
|---|
| | 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"``. |
|---|
| | 459 | This specifies how to order the results. |
|---|
| | 460 | |
|---|
| | 461 | For 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 | |
|---|
| | 477 | Relations are the bread and butter of databases, so there's an option to "follow" |
|---|
| | 478 | all relationships and pre-fill them in a simple cache so that later calls to |
|---|
| | 479 | objects with a one-to-many relationship don't have to hit the database. Do this by |
|---|
| | 480 | passing ``select_related=True`` to a lookup. This results in (sometimes much) larger |
|---|
| | 481 | queries, but it means that later use of relationships is much faster. |
|---|
| | 482 | |
|---|
| | 483 | For 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 | |
|---|
| | 487 | Then subsequent calls to ``c.poll`` won't hit the database. |
|---|
| | 488 | |
|---|
| | 489 | Note that ``select_related`` follows foreign keys as far as possible. If you have the |
|---|
| | 490 | following 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 | |
|---|
| | 503 | then a call to ``SingleVotes.objects.select_related().get(id=4)`` will |
|---|
| | 504 | cache 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 | |
|---|
| | 518 | Sometimes, the Django query syntax by itself isn't quite enough. To cater for these |
|---|
| | 519 | edge cases, Django provides the ``extra()`` Query Set modifier - a mechanism |
|---|
| | 520 | for injecting specific clauses into the SQL generated by a Query Set. |
|---|
| | 521 | |
|---|
| | 522 | Note that by definition these extra lookups may not be portable to different |
|---|
| | 523 | database engines (because you're explicitly writing SQL code) and should be |
|---|
| | 524 | avoided 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 | |
|---|
| | 567 | Caching and QuerySets |
|---|
| | 568 | --------------------- |
|---|
| | 569 | |
|---|
| | 570 | Each ``QuerySet`` contains a cache, to minimize database access. |
|---|
| | 571 | |
|---|
| | 572 | In a newly created ``QuerySet``, this cache is empty. The first time a |
|---|
| | 573 | ``QuerySet`` is evaluated -- and, hence, a database query happens -- Django |
|---|
| | 574 | saves the query results in the ``QuerySet``'s cache and returns the results |
|---|
| | 575 | that 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 | |
|---|
| | 579 | Keep this caching behavior in mind, because it may bite you if you don't use |
|---|
| | 580 | your ``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 | |
|---|
| | 586 | That means the same database query will be executed twice, effectively doubling |
|---|
| | 587 | your database load. Also, there's a possibility the two lists may not include |
|---|
| | 588 | the same database records, because an ``Entry`` may have been added or deleted |
|---|
| | 589 | in the split second between the two requests. |
|---|
| | 590 | |
|---|
| | 591 | To 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 | |
|---|
| | 599 | Deleting objects |
|---|
| | 600 | ================ |
|---|
| | 601 | |
|---|
| | 602 | |
|---|
| | 603 | |
|---|
| 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 | | |
|---|