|
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(maxlength=100) |
|
16 tagline = models.TextField() |
|
17 |
|
18 def __str__(self): |
|
19 return self.name |
|
20 |
|
21 class Author(models.Model): |
|
22 name = models.CharField(maxlength=50) |
|
23 email = models.URLField() |
|
24 |
|
25 def __str__(self): |
|
26 return self.name |
|
27 |
|
28 class Entry(models.Model): |
|
29 blog = models.ForeignKey(Blog) |
|
30 headline = models.CharField(maxlength=255) |
|
31 body_text = models.TextField() |
|
32 pub_date = models.DateTimeField() |
|
33 authors = models.ManyToManyField(Author) |
|
34 |
|
35 def __str__(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 Saving changes to objects |
|
122 ========================= |
|
123 |
|
124 To save changes to an object that's already in the database, use ``save()``. |
|
125 |
|
126 Given a ``Blog`` instance ``b5`` that has already been saved to the database, |
|
127 this example changes its name and updates its record in the database:: |
|
128 |
|
129 b5.name = 'New name' |
|
130 b5.save() |
|
131 |
|
132 This performs an ``UPDATE`` SQL statement behind the scenes. Django doesn't hit |
|
133 the database until you explicitly call ``save()``. |
|
134 |
|
135 The ``save()`` method has no return value. |
|
136 |
|
137 How Django knows to UPDATE vs. INSERT |
|
138 ------------------------------------- |
|
139 |
|
140 You may have noticed Django database objects use the same ``save()`` method |
|
141 for creating and changing objects. Django abstracts the need to use ``INSERT`` |
|
142 or ``UPDATE`` SQL statements. Specifically, when you call ``save()``, Django |
|
143 follows this algorithm: |
|
144 |
|
145 * If the object's primary key attribute is set to a value that evaluates to |
|
146 ``True`` (i.e., a value other than ``None`` or the empty string), Django |
|
147 executes a ``SELECT`` query to determine whether a record with the given |
|
148 primary key already exists. |
|
149 * If the record with the given primary key does already exist, Django |
|
150 executes an ``UPDATE`` query. |
|
151 * If the object's primary key attribute is *not* set, or if it's set but a |
|
152 record doesn't exist, Django executes an ``INSERT``. |
|
153 |
|
154 The one gotcha here is that you should be careful not to specify a primary-key |
|
155 value explicitly when saving new objects, if you cannot guarantee the |
|
156 primary-key value is unused. For more on this nuance, see |
|
157 "Explicitly specifying auto-primary-key values" above. |
|
158 |
|
159 Retrieving objects |
|
160 ================== |
|
161 |
|
162 To retrieve objects from your database, you construct a ``QuerySet`` via a |
|
163 ``Manager`` on your model class. |
|
164 |
|
165 A ``QuerySet`` represents a collection of objects from your database. It can |
|
166 have zero, one or many *filters* -- criteria that narrow down the collection |
|
167 based on given parameters. In SQL terms, a ``QuerySet`` equates to a ``SELECT`` |
|
168 statement, and a filter is a limiting clause such as ``WHERE`` or ``LIMIT``. |
|
169 |
|
170 You get a ``QuerySet`` by using your model's ``Manager``. Each model has at |
|
171 least one ``Manager``, and it's called ``objects`` by default. Access it |
|
172 directly via the model class, like so:: |
|
173 |
|
174 Blog.objects # <django.db.models.manager.Manager object at ...> |
|
175 b = Blog(name='Foo', tagline='Bar') |
|
176 b.objects # AttributeError: "Manager isn't accessible via Blog instances." |
|
177 |
|
178 (``Managers`` are accessible only via model classes, rather than from model |
|
179 instances, to enforce a separation between "table-level" operations and |
|
180 "record-level" operations.) |
|
181 |
|
182 The ``Manager`` is the main source of ``QuerySets`` for a model. It acts as a |
|
183 "root" ``QuerySet`` that describes all objects in the model's database table. |
|
184 For example, ``Blog.objects`` is the initial ``QuerySet`` that contains all |
|
185 ``Blog`` objects in the database. |
|
186 |
|
187 Retrieving all objects |
|
188 ---------------------- |
|
189 |
|
190 The simplest way to retrieve objects from a table is to get all of them. |
|
191 To do this, use the ``all()`` method on a ``Manager``. |
|
192 |
|
193 Example:: |
|
194 |
|
195 all_entries = Entry.objects.all() |
|
196 |
|
197 The ``all()`` method returns a ``QuerySet`` of all the objects in the database. |
|
198 |
|
199 (If ``Entry.objects`` is a ``QuerySet``, why can't we just do ``Entry.objects``? |
|
200 That's because ``Entry.objects``, the root ``QuerySet``, is a special case |
|
201 that cannot be evaluated. The ``all()`` method returns a ``QuerySet`` that |
|
202 *can* be evaluated.) |
|
203 |
|
204 Filtering objects |
|
205 ----------------- |
|
206 |
|
207 The root ``QuerySet`` provided by the ``Manager`` describes all objects in the |
|
208 database table. Usually, though, you'll need to select only a subset of the |
|
209 complete set of objects. |
|
210 |
|
211 To create such a subset, you refine the initial ``QuerySet``, adding filter |
|
212 conditions. The two most common ways to refine a ``QuerySet`` are: |
|
213 |
|
214 ``filter(**kwargs)`` |
|
215 Returns a new ``QuerySet`` containing objects that match the given lookup |
|
216 parameters. |
|
217 |
|
218 ``exclude(**kwargs)`` |
|
219 Returns a new ``QuerySet`` containing objects that do *not* match the given |
|
220 lookup parameters. |
|
221 |
|
222 The lookup parameters (``**kwargs`` in the above function definitions) should |
|
223 be in the format described in `Field lookups`_ below. |
|
224 |
|
225 For example, to get a ``QuerySet`` of blog entries from the year 2006, use |
|
226 ``filter()`` like so:: |
|
227 |
|
228 Entry.objects.filter(pub_date__year=2006) |
|
229 |
|
230 (Note we don't have to add an ``all()`` -- ``Entry.objects.all().filter(...)``. |
|
231 That would still work, but you only need ``all()`` when you want all objects |
|
232 from the root ``QuerySet``.) |
|
233 |
|
234 Chaining filters |
|
235 ~~~~~~~~~~~~~~~~ |
|
236 |
|
237 The result of refining a ``QuerySet`` is itself a ``QuerySet``, so it's |
|
238 possible to chain refinements together. For example:: |
|
239 |
|
240 Entry.objects.filter( |
|
241 headline__startswith='What').exclude( |
|
242 pub_date__gte=datetime.now()).filter( |
|
243 pub_date__gte=datetime(2005, 1, 1)) |
|
244 |
|
245 ...takes the initial ``QuerySet`` of all entries in the database, adds a |
|
246 filter, then an exclusion, then another filter. The final result is a |
|
247 ``QuerySet`` containing all entries with a headline that starts with "What", |
|
248 that were published between January 1, 2005, and the current day. |
|
249 |
|
250 Filtered QuerySets are unique |
|
251 ----------------------------- |
|
252 |
|
253 Each time you refine a ``QuerySet``, you get a brand-new ``QuerySet`` that is |
|
254 in no way bound to the previous ``QuerySet``. Each refinement creates a |
|
255 separate and distinct ``QuerySet`` that can be stored, used and reused. |
|
256 |
|
257 Example:: |
|
258 |
|
259 q1 = Entry.objects.filter(headline__startswith="What") |
|
260 q2 = q1.exclude(pub_date__gte=datetime.now()) |
|
261 q3 = q1.filter(pub_date__gte=datetime.now()) |
|
262 |
|
263 These three ``QuerySets`` are separate. The first is a base ``QuerySet`` |
|
264 containing all entries that contain a headline starting with "What". The second |
|
265 is a subset of the first, with an additional criteria that excludes records |
|
266 whose ``pub_date`` is greater than now. The third is a subset of the first, |
|
267 with an additional criteria that selects only the records whose ``pub_date`` is |
|
268 greater than now. The initial ``QuerySet`` (``q1``) is unaffected by the |
|
269 refinement process. |
|
270 |
|
271 QuerySets are lazy |
|
272 ------------------ |
|
273 |
|
274 ``QuerySets`` are lazy -- the act of creating a ``QuerySet`` doesn't involve |
|
275 any database activity. You can stack filters together all day long, and Django |
|
276 won't actually run the query until the ``QuerySet`` is *evaluated*. |
|
277 |
|
278 When QuerySets are evaluated |
|
279 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
280 |
|
281 You can evaluate a ``QuerySet`` in the following ways: |
|
282 |
|
283 * **Iteration.** A ``QuerySet`` is iterable, and it executes its database |
|
284 query the first time you iterate over it. For example, this will print |
|
285 the headline of all entries in the database:: |
|
286 |
|
287 for e in Entry.objects.all(): |
|
288 print e.headline |
|
289 |
|
290 * **Slicing.** As explained in `Limiting QuerySets`_ below, a ``QuerySet`` |
|
291 can be sliced, using Python's array-slicing syntax. Usually slicing a |
|
292 ``QuerySet`` returns another (unevaluated )``QuerySet``, but Django will |
|
293 execute the database query if you use the "step" parameter of slice |
|
294 syntax. |
|
295 |
|
296 * **repr().** A ``QuerySet`` is evaluated when you call ``repr()`` on it. |
|
297 This is for convenience in the Python interactive interpreter, so you can |
|
298 immediately see your results when using the API interactively. |
|
299 |
|
300 * **len().** A ``QuerySet`` is evaluated when you call ``len()`` on it. |
|
301 This, as you might expect, returns the length of the result list. |
|
302 |
|
303 Note: *Don't* use ``len()`` on ``QuerySet``\s if all you want to do is |
|
304 determine the number of records in the set. It's much more efficient to |
|
305 handle a count at the database level, using SQL's ``SELECT COUNT(*)``, |
|
306 and Django provides a ``count()`` method for precisely this reason. See |
|
307 ``count()`` below. |
|
308 |
|
309 * **list().** Force evaluation of a ``QuerySet`` by calling ``list()`` on |
|
310 it. For example:: |
|
311 |
|
312 entry_list = list(Entry.objects.all()) |
|
313 |
|
314 Be warned, though, that this could have a large memory overhead, because |
|
315 Django will load each element of the list into memory. In contrast, |
|
316 iterating over a ``QuerySet`` will take advantage of your database to |
|
317 load data and instantiate objects only as you need them. |
|
318 |
|
319 Limiting QuerySets |
|
320 ------------------ |
|
321 |
|
322 Use Python's array-slicing syntax to limit your ``QuerySet`` to a certain |
|
323 number of results. This is the equivalent of SQL's ``LIMIT`` and ``OFFSET`` |
|
324 clauses. |
|
325 |
|
326 For example, this returns the first 5 objects (``LIMIT 5``):: |
|
327 |
|
328 Entry.objects.all()[:5] |
|
329 |
|
330 This returns the fifth through tenth objects (``OFFSET 5 LIMIT 5``):: |
|
331 |
|
332 Entry.objects.all()[5:10] |
|
333 |
|
334 Generally, slicing a ``QuerySet`` returns a new ``QuerySet`` -- it doesn't |
|
335 evaluate the query. An exception is if you use the "step" parameter of Python |
|
336 slice syntax. For example, this would actually execute the query in order to |
|
337 return a list of every *second* object of the first 10:: |
|
338 |
|
339 Entry.objects.all()[:10:2] |
|
340 |
|
341 To retrieve a *single* object rather than a list |
|
342 (e.g. ``SELECT foo FROM bar LIMIT 1``), use a simple index instead of a |
|
343 slice. For example, this returns the first ``Entry`` in the database, after |
|
344 ordering entries alphabetically by headline:: |
|
345 |
|
346 Entry.objects.order_by('headline')[0] |
|
347 |
|
348 This is roughly equivalent to:: |
|
349 |
|
350 Entry.objects.order_by('headline')[0:1].get() |
|
351 |
|
352 Note, however, that the first of these will raise ``IndexError`` while the |
|
353 second will raise ``DoesNotExist`` if no objects match the given criteria. |
|
354 |
|
355 QuerySet methods that return new QuerySets |
|
356 ------------------------------------------ |
|
357 |
|
358 Django provides a range of ``QuerySet`` refinement methods that modify either |
|
359 the types of results returned by the ``QuerySet`` or the way its SQL query is |
|
360 executed. |
|
361 |
|
362 ``filter(**kwargs)`` |
|
363 ~~~~~~~~~~~~~~~~~~~~ |
|
364 |
|
365 Returns a new ``QuerySet`` containing objects that match the given lookup |
|
366 parameters. |
|
367 |
|
368 The lookup parameters (``**kwargs``) should be in the format described in |
|
369 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the |
|
370 underlying SQL statement. |
|
371 |
|
372 ``exclude(**kwargs)`` |
|
373 ~~~~~~~~~~~~~~~~~~~~~ |
|
374 |
|
375 Returns a new ``QuerySet`` containing objects that do *not* match the given |
|
376 lookup parameters. |
|
377 |
|
378 The lookup parameters (``**kwargs``) should be in the format described in |
|
379 `Field lookups`_ below. Multiple parameters are joined via ``AND`` in the |
|
380 underlying SQL statement, and the whole thing is enclosed in a ``NOT()``. |
|
381 |
|
382 This example excludes all entries whose ``pub_date`` is the current date/time |
|
383 AND whose ``headline`` is "Hello":: |
|
384 |
|
385 Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3), headline='Hello') |
|
386 |
|
387 In SQL terms, that evaluates to:: |
|
388 |
|
389 SELECT ... |
|
390 WHERE NOT (pub_date > '2005-1-3' AND headline = 'Hello') |
|
391 |
|
392 This example excludes all entries whose ``pub_date`` is the current date/time |
|
393 OR whose ``headline`` is "Hello":: |
|
394 |
|
395 Entry.objects.exclude(pub_date__gt=datetime.date(2005, 1, 3)).exclude(headline='Hello') |
|
396 |
|
397 In SQL terms, that evaluates to:: |
|
398 |
|
399 SELECT ... |
|
400 WHERE NOT pub_date > '2005-1-3' |
|
401 AND NOT headline = 'Hello' |
|
402 |
|
403 Note the second example is more restrictive. |
|
404 |
|
405 ``order_by(*fields)`` |
|
406 ~~~~~~~~~~~~~~~~~~~~~ |
|
407 |
|
408 By default, results returned by a ``QuerySet`` are ordered by the ordering |
|
409 tuple given by the ``ordering`` option in the model's ``Meta``. You can |
|
410 override this on a per-``QuerySet`` basis by using the ``order_by`` method. |
|
411 |
|
412 Example:: |
|
413 |
|
414 Entry.objects.filter(pub_date__year=2005).order_by('-pub_date', 'headline') |
|
415 |
|
416 The result above will be ordered by ``pub_date`` descending, then by |
|
417 ``headline`` ascending. The negative sign in front of ``"-pub_date"`` indicates |
|
418 *descending* order. Ascending order is implied. To order randomly, use ``"?"``, |
|
419 like so:: |
|
420 |
|
421 Entry.objects.order_by('?') |
|
422 |
|
423 To order by a field in a different table, add the other table's name and a dot, |
|
424 like so:: |
|
425 |
|
426 Entry.objects.order_by('blogs_blog.name', 'headline') |
|
427 |
|
428 There's no way to specify whether ordering should be case sensitive. With |
|
429 respect to case-sensitivity, Django will order results however your database |
|
430 backend normally orders them. |
|
431 |
|
432 ``distinct()`` |
|
433 ~~~~~~~~~~~~~~ |
|
434 |
|
435 Returns a new ``QuerySet`` that uses ``SELECT DISTINCT`` in its SQL query. This |
|
436 eliminates duplicate rows from the query results. |
|
437 |
|
438 By default, a ``QuerySet`` will not eliminate duplicate rows. In practice, this |
|
439 is rarely a problem, because simple queries such as ``Blog.objects.all()`` |
|
440 don't introduce the possibility of duplicate result rows. |
|
441 |
|
442 However, if your query spans multiple tables, it's possible to get duplicate |
|
443 results when a ``QuerySet`` is evaluated. That's when you'd use ``distinct()``. |
|
444 |
|
445 ``values(*fields)`` |
|
446 ~~~~~~~~~~~~~~~~~~~ |
|
447 |
|
448 Returns a ``ValuesQuerySet`` -- a ``QuerySet`` that evaluates to a list of |
|
449 dictionaries instead of model-instance objects. |
|
450 |
|
451 Each of those dictionaries represents an object, with the keys corresponding to |
|
452 the attribute names of model objects. |
|
453 |
|
454 This example compares the dictionaries of ``values()`` with the normal model |
|
455 objects:: |
|
456 |
|
457 # This list contains a Blog object. |
|
458 >>> Blog.objects.filter(name__startswith='Beatles') |
|
459 [Beatles Blog] |
|
460 |
|
461 # This list contains a dictionary. |
|
462 >>> Blog.objects.filter(name__startswith='Beatles').values() |
|
463 [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}] |
|
464 |
|
465 ``values()`` takes optional positional arguments, ``*fields``, which specify |
|
466 field names to which the ``SELECT`` should be limited. If you specify the |
|
467 fields, each dictionary will contain only the field keys/values for the fields |
|
468 you specify. If you don't specify the fields, each dictionary will contain a |
|
469 key and value for every field in the database table. |
|
470 |
|
471 Example:: |
|
472 |
|
473 >>> Blog.objects.values() |
|
474 [{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}], |
|
475 >>> Blog.objects.values('id', 'name') |
|
476 [{'id': 1, 'name': 'Beatles Blog'}] |
|
477 |
|
478 A ``ValuesQuerySet`` is useful when you know you're only going to need values |
|
479 from a small number of the available fields and you won't need the |
|
480 functionality of a model instance object. It's more efficient to select only |
|
481 the fields you need to use. |
|
482 |
|
483 Finally, note a ``ValuesQuerySet`` is a subclass of ``QuerySet``, so it has all |
|
484 methods of ``QuerySet``. You can call ``filter()`` on it, or ``order_by()``, or |
|
485 whatever. Yes, that means these two calls are identical:: |
|
486 |
|
487 Blog.objects.values().order_by('id') |
|
488 Blog.objects.order_by('id').values() |
|
489 |
|
490 The people who made Django prefer to put all the SQL-affecting methods first, |
|
491 followed (optionally) by any output-affecting methods (such as ``values()``), |
|
492 but it doesn't really matter. This is your chance to really flaunt your |
|
493 individualism. |
|
494 |
|
495 ``dates(field, kind, order='ASC')`` |
|
496 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
497 |
|
498 Returns a ``DateQuerySet`` -- a ``QuerySet`` that evaluates to a list of |
|
499 ``datetime.datetime`` objects representing all available dates of a particular |
|
500 kind within the contents of the ``QuerySet``. |
|
501 |
|
502 ``field`` should be the name of a ``DateField`` or ``DateTimeField`` of your |
|
503 model. |
|
504 |
|
505 ``kind`` should be either ``"year"``, ``"month"`` or ``"day"``. Each |
|
506 ``datetime.datetime`` object in the result list is "truncated" to the given |
|
507 ``type``. |
|
508 |
|
509 * ``"year"`` returns a list of all distinct year values for the field. |
|
510 * ``"month"`` returns a list of all distinct year/month values for the field. |
|
511 * ``"day"`` returns a list of all distinct year/month/day values for the field. |
|
512 |
|
513 ``order``, which defaults to ``'ASC'``, should be either ``'ASC'`` or |
|
514 ``'DESC'``. This specifies how to order the results. |
|
515 |
|
516 Examples:: |
|
517 |
|
518 >>> Entry.objects.dates('pub_date', 'year') |
|
519 [datetime.datetime(2005, 1, 1)] |
|
520 >>> Entry.objects.dates('pub_date', 'month') |
|
521 [datetime.datetime(2005, 2, 1), datetime.datetime(2005, 3, 1)] |
|
522 >>> Entry.objects.dates('pub_date', 'day') |
|
523 [datetime.datetime(2005, 2, 20), datetime.datetime(2005, 3, 20)] |
|
524 >>> Entry.objects.dates('pub_date', 'day', order='DESC') |
|
525 [datetime.datetime(2005, 3, 20), datetime.datetime(2005, 2, 20)] |
|
526 >>> Entry.objects.filter(headline__contains='Lennon').dates('pub_date', 'day') |
|
527 [datetime.datetime(2005, 3, 20)] |
|
528 |
|
529 ``none()`` |
|
530 ~~~~~~~~~~ |
|
531 |
|
532 **New in Django development version** |
|
533 |
|
534 Returns an ``EmptyQuerySet`` -- a ``QuerySet`` that always evaluates to |
|
535 an empty list. This can be used in cases where you know that you should |
|
536 return an empty result set and your caller is expecting a ``QuerySet`` |
|
537 object (instead of returning an empty list, for example.) |
|
538 |
|
539 Examples:: |
|
540 |
|
541 >>> Entry.objects.none() |
|
542 [] |
|
543 |
|
544 ``select_related()`` |
|
545 ~~~~~~~~~~~~~~~~~~~~ |
|
546 |
|
547 Returns a ``QuerySet`` that will automatically "follow" foreign-key |
|
548 relationships, selecting that additional related-object data when it executes |
|
549 its query. This is a performance booster which results in (sometimes much) |
|
550 larger queries but means later use of foreign-key relationships won't require |
|
551 database queries. |
|
552 |
|
553 The following examples illustrate the difference between plain lookups and |
|
554 ``select_related()`` lookups. Here's standard lookup:: |
|
555 |
|
556 # Hits the database. |
|
557 e = Entry.objects.get(id=5) |
|
558 |
|
559 # Hits the database again to get the related Blog object. |
|
560 b = e.blog |
|
561 |
|
562 And here's ``select_related`` lookup:: |
|
563 |
|
564 # Hits the database. |
|
565 e = Entry.objects.select_related().get(id=5) |
|
566 |
|
567 # Doesn't hit the database, because e.blog has been prepopulated |
|
568 # in the previous query. |
|
569 b = e.blog |
|
570 |
|
571 ``select_related()`` follows foreign keys as far as possible. If you have the |
|
572 following models:: |
|
573 |
|
574 class City(models.Model): |
|
575 # ... |
|
576 |
|
577 class Person(models.Model): |
|
578 # ... |
|
579 hometown = models.ForeignKey(City) |
|
580 |
|
581 class Book(models.Model): |
|
582 # ... |
|
583 author = models.ForeignKey(Person) |
|
584 |
|
585 ...then a call to ``Book.objects.select_related().get(id=4)`` will cache the |
|
586 related ``Person`` *and* the related ``City``:: |
|
587 |
|
588 b = Book.objects.select_related().get(id=4) |
|
589 p = b.author # Doesn't hit the database. |
|
590 c = p.hometown # Doesn't hit the database. |
|
591 |
|
592 sv = Book.objects.get(id=4) # No select_related() in this example. |
|
593 p = b.author # Hits the database. |
|
594 c = p.hometown # Hits the database. |
|
595 |
|
596 Note that ``select_related()`` does not follow foreign keys that have |
|
597 ``null=True``. |
|
598 |
|
599 Usually, using ``select_related()`` can vastly improve performance because your |
|
600 app can avoid many database calls. However, in situations with deeply nested |
|
601 sets of relationships ``select_related()`` can sometimes end up following "too |
|
602 many" relations, and can generate queries so large that they end up being slow. |
|
603 |
|
604 In these situations, you can use the ``depth`` argument to ``select_related()`` |
|
605 to control how many "levels" of relations ``select_related()`` will actually |
|
606 follow:: |
|
607 |
|
608 b = Book.objects.select_related(depth=1).get(id=4) |
|
609 p = b.author # Doesn't hit the database. |
|
610 c = p.hometown # Requires a database call. |
|
611 |
|
612 The ``depth`` argument is new in the Django development version. |
|
613 |
|
614 ``extra(select=None, where=None, params=None, tables=None)`` |
|
615 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
616 |
|
617 Sometimes, the Django query syntax by itself can't easily express a complex |
|
618 ``WHERE`` clause. For these edge cases, Django provides the ``extra()`` |
|
619 ``QuerySet`` modifier -- a hook for injecting specific clauses into the SQL |
|
620 generated by a ``QuerySet``. |
|
621 |
|
622 By definition, these extra lookups may not be portable to different database |
|
623 engines (because you're explicitly writing SQL code) and violate the DRY |
|
624 principle, so you should avoid them if possible. |
|
625 |
|
626 Specify one or more of ``params``, ``select``, ``where`` or ``tables``. None |
|
627 of the arguments is required, but you should use at least one of them. |
|
628 |
|
629 ``select`` |
|
630 The ``select`` argument lets you put extra fields in the ``SELECT`` clause. |
|
631 It should be a dictionary mapping attribute names to SQL clauses to use to |
|
632 calculate that attribute. |
|
633 |
|
634 Example:: |
|
635 |
|
636 Entry.objects.extra(select={'is_recent': "pub_date > '2006-01-01'"}) |
|
637 |
|
638 As a result, each ``Entry`` object will have an extra attribute, |
|
639 ``is_recent``, a boolean representing whether the entry's ``pub_date`` is |
|
640 greater than Jan. 1, 2006. |
|
641 |
|
642 Django inserts the given SQL snippet directly into the ``SELECT`` |
|
643 statement, so the resulting SQL of the above example would be:: |
|
644 |
|
645 SELECT blog_entry.*, (pub_date > '2006-01-01') |
|
646 FROM blog_entry; |
|
647 |
|
648 |
|
649 The next example is more advanced; it does a subquery to give each |
|
650 resulting ``Blog`` object an ``entry_count`` attribute, an integer count |
|
651 of associated ``Entry`` objects:: |
|
652 |
|
653 Blog.objects.extra( |
|
654 select={ |
|
655 'entry_count': 'SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id' |
|
656 }, |
|
657 ) |
|
658 |
|
659 (In this particular case, we're exploiting the fact that the query will |
|
660 already contain the ``blog_blog`` table in its ``FROM`` clause.) |
|
661 |
|
662 The resulting SQL of the above example would be:: |
|
663 |
|
664 SELECT blog_blog.*, (SELECT COUNT(*) FROM blog_entry WHERE blog_entry.blog_id = blog_blog.id) |
|
665 FROM blog_blog; |
|
666 |
|
667 Note that the parenthesis required by most database engines around |
|
668 subqueries are not required in Django's ``select`` clauses. Also note that |
|
669 some database backends, such as some MySQL versions, don't support |
|
670 subqueries. |
|
671 |
|
672 ``where`` / ``tables`` |
|
673 You can define explicit SQL ``WHERE`` clauses -- perhaps to perform |
|
674 non-explicit joins -- by using ``where``. You can manually add tables to |
|
675 the SQL ``FROM`` clause by using ``tables``. |
|
676 |
|
677 ``where`` and ``tables`` both take a list of strings. All ``where`` |
|
678 parameters are "AND"ed to any other search criteria. |
|
679 |
|
680 Example:: |
|
681 |
|
682 Entry.objects.extra(where=['id IN (3, 4, 5, 20)']) |
|
683 |
|
684 ...translates (roughly) into the following SQL:: |
|
685 |
|
686 SELECT * FROM blog_entry WHERE id IN (3, 4, 5, 20); |
|
687 |
|
688 ``params`` |
|
689 The ``select`` and ``where`` parameters described above may use standard |
|
690 Python database string placeholders -- ``'%s'`` to indicate parameters the |
|
691 database engine should automatically quote. The ``params`` argument is a |
|
692 list of any extra parameters to be substituted. |
|
693 |
|
694 Example:: |
|
695 |
|
696 Entry.objects.extra(where=['headline=%s'], params=['Lennon']) |
|
697 |
|
698 Always use ``params`` instead of embedding values directly into ``select`` |
|
699 or ``where`` because ``params`` will ensure values are quoted correctly |
|
700 according to your particular backend. (For example, quotes will be escaped |
|
701 correctly.) |
|
702 |
|
703 Bad:: |
|
704 |
|
705 Entry.objects.extra(where=["headline='Lennon'"]) |
|
706 |
|
707 Good:: |
|
708 |
|
709 Entry.objects.extra(where=['headline=%s'], params=['Lennon']) |
|
710 |
|
711 QuerySet methods that do not return QuerySets |
|
712 --------------------------------------------- |
|
713 |
|
714 The following ``QuerySet`` methods evaluate the ``QuerySet`` and return |
|
715 something *other than* a ``QuerySet``. |
|
716 |
|
717 These methods do not use a cache (see _`Caching and QuerySets` below). Rather, |
|
718 they query the database each time they're called. |
|
719 |
|
720 ``get(**kwargs)`` |
|
721 ~~~~~~~~~~~~~~~~~ |
|
722 |
|
723 Returns the object matching the given lookup parameters, which should be in |
|
724 the format described in `Field lookups`_. |
|
725 |
|
726 ``get()`` raises ``AssertionError`` if more than one object was found. |
|
727 |
|
728 ``get()`` raises a ``DoesNotExist`` exception if an object wasn't found for the |
|
729 given parameters. The ``DoesNotExist`` exception is an attribute of the model |
|
730 class. Example:: |
|
731 |
|
732 Entry.objects.get(id='foo') # raises Entry.DoesNotExist |
|
733 |
|
734 The ``DoesNotExist`` exception inherits from |
|
735 ``django.core.exceptions.ObjectDoesNotExist``, so you can target multiple |
|
736 ``DoesNotExist`` exceptions. Example:: |
|
737 |
|
738 from django.core.exceptions import ObjectDoesNotExist |
|
739 try: |
|
740 e = Entry.objects.get(id=3) |
|
741 b = Blog.objects.get(id=1) |
|
742 except ObjectDoesNotExist: |
|
743 print "Either the entry or blog doesn't exist." |
|
744 |
|
745 ``create(**kwargs)`` |
|
746 ~~~~~~~~~~~~~~~~~~~~ |
|
747 |
|
748 A convenience method for creating an object and saving it all in one step. Thus:: |
|
749 |
|
750 p = Person.objects.create(first_name="Bruce", last_name="Springsteen") |
|
751 |
|
752 and:: |
|
753 |
|
754 p = Person(first_name="Bruce", last_name="Springsteen") |
|
755 p.save() |
|
756 |
|
757 are equivalent. |
|
758 |
|
759 ``get_or_create(**kwargs)`` |
|
760 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
761 |
|
762 A convenience method for looking up an object with the given kwargs, creating |
|
763 one if necessary. |
|
764 |
|
765 Returns a tuple of ``(object, created)``, where ``object`` is the retrieved or |
|
766 created object and ``created`` is a boolean specifying whether a new object was |
|
767 created. |
|
768 |
|
769 This is meant as a shortcut to boilerplatish code and is mostly useful for |
|
770 data-import scripts. For example:: |
|
771 |
|
772 try: |
|
773 obj = Person.objects.get(first_name='John', last_name='Lennon') |
|
774 except Person.DoesNotExist: |
|
775 obj = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)) |
|
776 obj.save() |
|
777 |
|
778 This pattern gets quite unwieldy as the number of fields in a model goes up. |
|
779 The above example can be rewritten using ``get_or_create()`` like so:: |
|
780 |
|
781 obj, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', |
|
782 defaults={'birthday': date(1940, 10, 9)}) |
|
783 |
|
784 Any keyword arguments passed to ``get_or_create()`` -- *except* an optional one |
|
785 called ``defaults`` -- will be used in a ``get()`` call. If an object is found, |
|
786 ``get_or_create()`` returns a tuple of that object and ``False``. If an object |
|
787 is *not* found, ``get_or_create()`` will instantiate and save a new object, |
|
788 returning a tuple of the new object and ``True``. The new object will be |
|
789 created according to this algorithm:: |
|
790 |
|
791 defaults = kwargs.pop('defaults', {}) |
|
792 params = dict([(k, v) for k, v in kwargs.items() if '__' not in k]) |
|
793 params.update(defaults) |
|
794 obj = self.model(**params) |
|
795 obj.save() |
|
796 |
|
797 In English, that means start with any non-``'defaults'`` keyword argument that |
|
798 doesn't contain a double underscore (which would indicate a non-exact lookup). |
|
799 Then add the contents of ``defaults``, overriding any keys if necessary, and |
|
800 use the result as the keyword arguments to the model class. |
|
801 |
|
802 If you have a field named ``defaults`` and want to use it as an exact lookup in |
|
803 ``get_or_create()``, just use ``'defaults__exact'``, like so:: |
|
804 |
|
805 Foo.objects.get_or_create(defaults__exact='bar', defaults={'defaults': 'baz'}) |
|
806 |
|
807 Finally, a word on using ``get_or_create()`` in Django views. As mentioned |
|
808 earlier, ``get_or_create()`` is mostly useful in scripts that need to parse |
|
809 data and create new records if existing ones aren't available. But if you need |
|
810 to use ``get_or_create()`` in a view, please make sure to use it only in |
|
811 ``POST`` requests unless you have a good reason not to. ``GET`` requests |
|
812 shouldn't have any effect on data; use ``POST`` whenever a request to a page |
|
813 has a side effect on your data. For more, see `Safe methods`_ in the HTTP spec. |
|
814 |
|
815 .. _Safe methods: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 |
|
816 |
|
817 ``count()`` |
|
818 ~~~~~~~~~~~ |
|
819 |
|
820 Returns an integer representing the number of objects in the database matching |
|
821 the ``QuerySet``. ``count()`` never raises exceptions. |
|
822 |
|
823 Example:: |
|
824 |
|
825 # Returns the total number of entries in the database. |
|
826 Entry.objects.count() |
|
827 |
|
828 # Returns the number of entries whose headline contains 'Lennon' |
|
829 Entry.objects.filter(headline__contains='Lennon').count() |
|
830 |
|
831 ``count()`` performs a ``SELECT COUNT(*)`` behind the scenes, so you should |
|
832 always use ``count()`` rather than loading all of the record into Python |
|
833 objects and calling ``len()`` on the result. |
|
834 |
|
835 Depending on which database you're using (e.g. PostgreSQL vs. MySQL), |
|
836 ``count()`` may return a long integer instead of a normal Python integer. This |
|
837 is an underlying implementation quirk that shouldn't pose any real-world |
|
838 problems. |
|
839 |
|
840 ``in_bulk(id_list)`` |
|
841 ~~~~~~~~~~~~~~~~~~~~ |
|
842 |
|
843 Takes a list of primary-key values and returns a dictionary mapping each |
|
844 primary-key value to an instance of the object with the given ID. |
|
845 |
|
846 Example:: |
|
847 |
|
848 >>> Blog.objects.in_bulk([1]) |
|
849 {1: Beatles Blog} |
|
850 >>> Blog.objects.in_bulk([1, 2]) |
|
851 {1: Beatles Blog, 2: Cheddar Talk} |
|
852 >>> Blog.objects.in_bulk([]) |
|
853 {} |
|
854 |
|
855 If you pass ``in_bulk()`` an empty list, you'll get an empty dictionary. |
|
856 |
|
857 ``latest(field_name=None)`` |
|
858 ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
859 |
|
860 Returns the latest object in the table, by date, using the ``field_name`` |
|
861 provided as the date field. |
|
862 |
|
863 This example returns the latest ``Entry`` in the table, according to the |
|
864 ``pub_date`` field:: |
|
865 |
|
866 Entry.objects.latest('pub_date') |
|
867 |
|
868 If your model's ``Meta`` specifies ``get_latest_by``, you can leave off the |
|
869 ``field_name`` argument to ``latest()``. Django will use the field specified in |
|
870 ``get_latest_by`` by default. |
|
871 |
|
872 Like ``get()``, ``latest()`` raises ``DoesNotExist`` if an object doesn't |
|
873 exist with the given parameters. |
|
874 |
|
875 Note ``latest()`` exists purely for convenience and readability. |
|
876 |
|
877 Field lookups |
|
878 ------------- |
|
879 |
|
880 Field lookups are how you specify the meat of an SQL ``WHERE`` clause. They're |
|
881 specified as keyword arguments to the ``QuerySet`` methods ``filter()``, |
|
882 ``exclude()`` and ``get()``. |
|
883 |
|
884 Basic lookups keyword arguments take the form ``field__lookuptype=value``. |
|
885 (That's a double-underscore). For example:: |
|
886 |
|
887 Entry.objects.filter(pub_date__lte='2006-01-01') |
|
888 |
|
889 translates (roughly) into the following SQL:: |
|
890 |
|
891 SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01'; |
|
892 |
|
893 .. admonition:: How this is possible |
|
894 |
|
895 Python has the ability to define functions that accept arbitrary name-value |
|
896 arguments whose names and values are evaluated at runtime. For more |
|
897 information, see `Keyword Arguments`_ in the official Python tutorial. |
|
898 |
|
899 .. _`Keyword Arguments`: http://docs.python.org/tut/node6.html#SECTION006720000000000000000 |
|
900 |
|
901 If you pass an invalid keyword argument, a lookup function will raise |
|
902 ``TypeError``. |
|
903 |
|
904 The database API supports the following lookup types: |
|
905 |
|
906 exact |
|
907 ~~~~~ |
|
908 |
|
909 Exact match. If the value provided for comparison is ``None``, it will |
|
910 be interpreted as an SQL ``NULL`` (See isnull_ for more details). |
|
911 |
|
912 Examples:: |
|
913 |
|
914 Entry.objects.get(id__exact=14) |
|
915 Entry.objects.get(id__exact=None) |
|
916 |
|
917 SQL equivalents:: |
|
918 |
|
919 SELECT ... WHERE id = 14; |
|
920 SELECT ... WHERE id = NULL; |
|
921 |
|
922 iexact |
|
923 ~~~~~~ |
|
924 |
|
925 Case-insensitive exact match. |
|
926 |
|
927 Example:: |
|
928 |
|
929 Blog.objects.get(name__iexact='beatles blog') |
|
930 |
|
931 SQL equivalent:: |
|
932 |
|
933 SELECT ... WHERE name ILIKE 'beatles blog'; |
|
934 |
|
935 Note this will match ``'Beatles Blog'``, ``'beatles blog'``, |
|
936 ``'BeAtLes BLoG'``, etc. |
|
937 |
|
938 contains |
|
939 ~~~~~~~~ |
|
940 |
|
941 Case-sensitive containment test. |
|
942 |
|
943 Example:: |
|
944 |
|
945 Entry.objects.get(headline__contains='Lennon') |
|
946 |
|
947 SQL equivalent:: |
|
948 |
|
949 SELECT ... WHERE headline LIKE '%Lennon%'; |
|
950 |
|
951 Note this will match the headline ``'Today Lennon honored'`` but not |
|
952 ``'today lennon honored'``. |
|
953 |
|
954 SQLite doesn't support case-sensitive ``LIKE`` statements; ``contains`` acts |
|
955 like ``icontains`` for SQLite. |
|
956 |
|
957 icontains |
|
958 ~~~~~~~~~ |
|
959 |
|
960 Case-insensitive containment test. |
|
961 |
|
962 Example:: |
|
963 |
|
964 Entry.objects.get(headline__icontains='Lennon') |
|
965 |
|
966 SQL equivalent:: |
|
967 |
|
968 SELECT ... WHERE headline ILIKE '%Lennon%'; |
|
969 |
|
970 gt |
|
971 ~~ |
|
972 |
|
973 Greater than. |
|
974 |
|
975 Example:: |
|
976 |
|
977 Entry.objects.filter(id__gt=4) |
|
978 |
|
979 SQL equivalent:: |
|
980 |
|
981 SELECT ... WHERE id > 4; |
|
982 |
|
983 gte |
|
984 ~~~ |
|
985 |
|
986 Greater than or equal to. |
|
987 |
|
988 lt |
|
989 ~~ |
|
990 |
|
991 Less than. |
|
992 |
|
993 lte |
|
994 ~~~ |
|
995 |
|
996 Less than or equal to. |
|
997 |
|
998 in |
|
999 ~~ |
|
1000 |
|
1001 In a given list. |
|
1002 |
|
1003 Example:: |
|
1004 |
|
1005 Entry.objects.filter(id__in=[1, 3, 4]) |
|
1006 |
|
1007 SQL equivalent:: |
|
1008 |
|
1009 SELECT ... WHERE id IN (1, 3, 4); |
|
1010 |
|
1011 startswith |
|
1012 ~~~~~~~~~~ |
|
1013 |
|
1014 Case-sensitive starts-with. |
|
1015 |
|
1016 Example:: |
|
1017 |
|
1018 Entry.objects.filter(headline__startswith='Will') |
|
1019 |
|
1020 SQL equivalent:: |
|
1021 |
|
1022 SELECT ... WHERE headline LIKE 'Will%'; |
|
1023 |
|
1024 SQLite doesn't support case-sensitive ``LIKE`` statements; ``startswith`` acts |
|
1025 like ``istartswith`` for SQLite. |
|
1026 |
|
1027 istartswith |
|
1028 ~~~~~~~~~~~ |
|
1029 |
|
1030 Case-insensitive starts-with. |
|
1031 |
|
1032 Example:: |
|
1033 |
|
1034 Entry.objects.filter(headline__istartswith='will') |
|
1035 |
|
1036 SQL equivalent:: |
|
1037 |
|
1038 SELECT ... WHERE headline ILIKE 'Will%'; |
|
1039 |
|
1040 endswith |
|
1041 ~~~~~~~~ |
|
1042 |
|
1043 Case-sensitive ends-with. |
|
1044 |
|
1045 Example:: |
|
1046 |
|
1047 Entry.objects.filter(headline__endswith='cats') |
|
1048 |
|
1049 SQL equivalent:: |
|
1050 |
|
1051 SELECT ... WHERE headline LIKE '%cats'; |
|
1052 |
|
1053 SQLite doesn't support case-sensitive ``LIKE`` statements; ``endswith`` acts |
|
1054 like ``iendswith`` for SQLite. |
|
1055 |
|
1056 iendswith |
|
1057 ~~~~~~~~~ |
|
1058 |
|
1059 Case-insensitive ends-with. |
|
1060 |
|
1061 Example:: |
|
1062 |
|
1063 Entry.objects.filter(headline__iendswith='will') |
|
1064 |
|
1065 SQL equivalent:: |
|
1066 |
|
1067 SELECT ... WHERE headline ILIKE '%will' |
|
1068 |
|
1069 range |
|
1070 ~~~~~ |
|
1071 |
|
1072 Range test (inclusive). |
|
1073 |
|
1074 Example:: |
|
1075 |
|
1076 start_date = datetime.date(2005, 1, 1) |
|
1077 end_date = datetime.date(2005, 3, 31) |
|
1078 Entry.objects.filter(pub_date__range=(start_date, end_date)) |
|
1079 |
|
1080 SQL equivalent:: |
|
1081 |
|
1082 SELECT ... WHERE pub_date BETWEEN '2005-01-01' and '2005-03-31'; |
|
1083 |
|
1084 You can use ``range`` anywhere you can use ``BETWEEN`` in SQL -- for dates, |
|
1085 numbers and even characters. |
|
1086 |
|
1087 year |
|
1088 ~~~~ |
|
1089 |
|
1090 For date/datetime fields, exact year match. Takes a four-digit year. |
|
1091 |
|
1092 Example:: |
|
1093 |
|
1094 Entry.objects.filter(pub_date__year=2005) |
|
1095 |
|
1096 SQL equivalent:: |
|
1097 |
|
1098 SELECT ... WHERE EXTRACT('year' FROM pub_date) = '2005'; |
|
1099 |
|
1100 (The exact SQL syntax varies for each database engine.) |
|
1101 |
|
1102 month |
|
1103 ~~~~~ |
|
1104 |
|
1105 For date/datetime fields, exact month match. Takes an integer 1 (January) |
|
1106 through 12 (December). |
|
1107 |
|
1108 Example:: |
|
1109 |
|
1110 Entry.objects.filter(pub_date__month=12) |
|
1111 |
|
1112 SQL equivalent:: |
|
1113 |
|
1114 SELECT ... WHERE EXTRACT('month' FROM pub_date) = '12'; |
|
1115 |
|
1116 (The exact SQL syntax varies for each database engine.) |
|
1117 |
|
1118 day |
|
1119 ~~~ |
|
1120 |
|
1121 For date/datetime fields, exact day match. |
|
1122 |
|
1123 Example:: |
|
1124 |
|
1125 Entry.objects.filter(pub_date__day=3) |
|
1126 |
|
1127 SQL equivalent:: |
|
1128 |
|
1129 SELECT ... WHERE EXTRACT('day' FROM pub_date) = '3'; |
|
1130 |
|
1131 (The exact SQL syntax varies for each database engine.) |
|
1132 |
|
1133 Note this will match any record with a pub_date on the third day of the month, |
|
1134 such as January 3, July 3, etc. |
|
1135 |
|
1136 isnull |
|
1137 ~~~~~~ |
|
1138 |
|
1139 Takes either ``True`` or ``False``, which correspond to SQL queries of |
|
1140 ``IS NULL`` and ``IS NOT NULL``, respectively. |
|
1141 |
|
1142 Example:: |
|
1143 |
|
1144 Entry.objects.filter(pub_date__isnull=True) |
|
1145 |
|
1146 SQL equivalent:: |
|
1147 |
|
1148 SELECT ... WHERE pub_date IS NULL; |
|
1149 |
|
1150 .. admonition:: ``__isnull=True`` vs ``__exact=None`` |
|
1151 |
|
1152 There is an important difference between ``__isnull=True`` and |
|
1153 ``__exact=None``. ``__exact=None`` will *always* return an empty result |
|
1154 set, because SQL requires that no value is equal to ``NULL``. |
|
1155 ``__isnull`` determines if the field is currently holding the value |
|
1156 of ``NULL`` without performing a comparison. |
|
1157 |
|
1158 search |
|
1159 ~~~~~~ |
|
1160 |
|
1161 A boolean full-text search, taking advantage of full-text indexing. This is |
|
1162 like ``contains`` but is significantly faster due to full-text indexing. |
|
1163 |
|
1164 Note this is only available in MySQL and requires direct manipulation of the |
|
1165 database to add the full-text index. |
|
1166 |
|
1167 Default lookups are exact |
|
1168 ------------------------- |
|
1169 |
|
1170 If you don't provide a lookup type -- that is, if your keyword argument doesn't |
|
1171 contain a double underscore -- the lookup type is assumed to be ``exact``. |
|
1172 |
|
1173 For example, the following two statements are equivalent:: |
|
1174 |
|
1175 Blog.objects.get(id__exact=14) # Explicit form |
|
1176 Blog.objects.get(id=14) # __exact is implied |
|
1177 |
|
1178 This is for convenience, because ``exact`` lookups are the common case. |
|
1179 |
|
1180 The pk lookup shortcut |
|
1181 ---------------------- |
|
1182 |
|
1183 For convenience, Django provides a ``pk`` lookup type, which stands for |
|
1184 "primary_key". |
|
1185 |
|
1186 In the example ``Blog`` model, the primary key is the ``id`` field, so these |
|
1187 three statements are equivalent:: |
|
1188 |
|
1189 Blog.objects.get(id__exact=14) # Explicit form |
|
1190 Blog.objects.get(id=14) # __exact is implied |
|
1191 Blog.objects.get(pk=14) # pk implies id__exact |
|
1192 |
|
1193 The use of ``pk`` isn't limited to ``__exact`` queries -- any query term |
|
1194 can be combined with ``pk`` to perform a query on the primary key of a model:: |
|
1195 |
|
1196 # Get blogs entries with id 1, 4 and 7 |
|
1197 Blog.objects.filter(pk__in=[1,4,7]) |
|
1198 # Get all blog entries with id > 14 |
|
1199 Blog.objects.filter(pk__gt=14) |
|
1200 |
|
1201 ``pk`` lookups also work across joins. For example, these three statements are |
|
1202 equivalent:: |
|
1203 |
|
1204 Entry.objects.filter(blog__id__exact=3) # Explicit form |
|
1205 Entry.objects.filter(blog__id=3) # __exact is implied |
|
1206 Entry.objects.filter(blog__pk=3) # __pk implies __id__exact |
|
1207 |
|
1208 Lookups that span relationships |
|
1209 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1210 |
|
1211 Django offers a powerful and intuitive way to "follow" relationships in |
|
1212 lookups, taking care of the SQL ``JOIN``\s for you automatically, behind the |
|
1213 scenes. To span a relationship, just use the field name of related fields |
|
1214 across models, separated by double underscores, until you get to the field you |
|
1215 want. |
|
1216 |
|
1217 This example retrieves all ``Entry`` objects with a ``Blog`` whose ``name`` |
|
1218 is ``'Beatles Blog'``:: |
|
1219 |
|
1220 Entry.objects.filter(blog__name__exact='Beatles Blog') |
|
1221 |
|
1222 This spanning can be as deep as you'd like. |
|
1223 |
|
1224 It works backwards, too. To refer to a "reverse" relationship, just use the |
|
1225 lowercase name of the model. |
|
1226 |
|
1227 This example retrieves all ``Blog`` objects which have at least one ``Entry`` |
|
1228 whose ``headline`` contains ``'Lennon'``:: |
|
1229 |
|
1230 Blog.objects.filter(entry__headline__contains='Lennon') |
|
1231 |
|
1232 Escaping parenthesis and underscores in LIKE statements |
|
1233 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1234 |
|
1235 The field lookups that equate to ``LIKE`` SQL statements (``iexact``, |
|
1236 ``contains``, ``icontains``, ``startswith``, ``istartswith``, ``endswith`` |
|
1237 and ``iendswith``) will automatically escape the two special characters used in |
|
1238 ``LIKE`` statements -- the percent sign and the underscore. (In a ``LIKE`` |
|
1239 statement, the percent sign signifies a multiple-character wildcard and the |
|
1240 underscore signifies a single-character wildcard.) |
|
1241 |
|
1242 This means things should work intuitively, so the abstraction doesn't leak. |
|
1243 For example, to retrieve all the entries that contain a percent sign, just use |
|
1244 the percent sign as any other character:: |
|
1245 |
|
1246 Entry.objects.filter(headline__contains='%') |
|
1247 |
|
1248 Django takes care of the quoting for you; the resulting SQL will look something |
|
1249 like this:: |
|
1250 |
|
1251 SELECT ... WHERE headline LIKE '%\%%'; |
|
1252 |
|
1253 Same goes for underscores. Both percentage signs and underscores are handled |
|
1254 for you transparently. |
|
1255 |
|
1256 Caching and QuerySets |
|
1257 --------------------- |
|
1258 |
|
1259 Each ``QuerySet`` contains a cache, to minimize database access. It's important |
|
1260 to understand how it works, in order to write the most efficient code. |
|
1261 |
|
1262 In a newly created ``QuerySet``, the cache is empty. The first time a |
|
1263 ``QuerySet`` is evaluated -- and, hence, a database query happens -- Django |
|
1264 saves the query results in the ``QuerySet``'s cache and returns the results |
|
1265 that have been explicitly requested (e.g., the next element, if the |
|
1266 ``QuerySet`` is being iterated over). Subsequent evaluations of the |
|
1267 ``QuerySet`` reuse the cached results. |
|
1268 |
|
1269 Keep this caching behavior in mind, because it may bite you if you don't use |
|
1270 your ``QuerySet``\s correctly. For example, the following will create two |
|
1271 ``QuerySet``\s, evaluate them, and throw them away:: |
|
1272 |
|
1273 print [e.headline for e in Entry.objects.all()] |
|
1274 print [e.pub_date for e in Entry.objects.all()] |
|
1275 |
|
1276 That means the same database query will be executed twice, effectively doubling |
|
1277 your database load. Also, there's a possibility the two lists may not include |
|
1278 the same database records, because an ``Entry`` may have been added or deleted |
|
1279 in the split second between the two requests. |
|
1280 |
|
1281 To avoid this problem, simply save the ``QuerySet`` and reuse it:: |
|
1282 |
|
1283 queryset = Poll.objects.all() |
|
1284 print [p.headline for p in queryset] # Evaluate the query set. |
|
1285 print [p.pub_date for p in queryset] # Re-use the cache from the evaluation. |
|
1286 |
|
1287 Comparing objects |
|
1288 ================= |
|
1289 |
|
1290 To compare two model instances, just use the standard Python comparison operator, |
|
1291 the double equals sign: ``==``. Behind the scenes, that compares the primary |
|
1292 key values of two models. |
|
1293 |
|
1294 Using the ``Entry`` example above, the following two statements are equivalent:: |
|
1295 |
|
1296 some_entry == other_entry |
|
1297 some_entry.id == other_entry.id |
|
1298 |
|
1299 If a model's primary key isn't called ``id``, no problem. Comparisons will |
|
1300 always use the primary key, whatever it's called. For example, if a model's |
|
1301 primary key field is called ``name``, these two statements are equivalent:: |
|
1302 |
|
1303 some_obj == other_obj |
|
1304 some_obj.name == other_obj.name |
|
1305 |
|
1306 Complex lookups with Q objects |
|
1307 ============================== |
|
1308 |
|
1309 Keyword argument queries -- in ``filter()``, etc. -- are "AND"ed together. If |
|
1310 you need to execute more complex queries (for example, queries with ``OR`` |
|
1311 statements), you can use ``Q`` objects. |
|
1312 |
|
1313 A ``Q`` object (``django.db.models.Q``) is an object used to encapsulate a |
|
1314 collection of keyword arguments. These keyword arguments are specified as in |
|
1315 "Field lookups" above. |
|
1316 |
|
1317 For example, this ``Q`` object encapsulates a single ``LIKE`` query:: |
|
1318 |
|
1319 Q(question__startswith='What') |
|
1320 |
|
1321 ``Q`` objects can be combined using the ``&`` and ``|`` operators. When an |
|
1322 operator is used on two ``Q`` objects, it yields a new ``Q`` object. |
|
1323 |
|
1324 For example, this statement yields a single ``Q`` object that represents the |
|
1325 "OR" of two ``"question__startswith"`` queries:: |
|
1326 |
|
1327 Q(question__startswith='Who') | Q(question__startswith='What') |
|
1328 |
|
1329 This is equivalent to the following SQL ``WHERE`` clause:: |
|
1330 |
|
1331 WHERE question LIKE 'Who%' OR question LIKE 'What%' |
|
1332 |
|
1333 You can compose statements of arbitrary complexity by combining ``Q`` objects |
|
1334 with the ``&`` and ``|`` operators. You can also use parenthetical grouping. |
|
1335 |
|
1336 Each lookup function that takes keyword-arguments (e.g. ``filter()``, |
|
1337 ``exclude()``, ``get()``) can also be passed one or more ``Q`` objects as |
|
1338 positional (not-named) arguments. If you provide multiple ``Q`` object |
|
1339 arguments to a lookup function, the arguments will be "AND"ed together. For |
|
1340 example:: |
|
1341 |
|
1342 Poll.objects.get( |
|
1343 Q(question__startswith='Who'), |
|
1344 Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)) |
|
1345 ) |
|
1346 |
|
1347 ... roughly translates into the SQL:: |
|
1348 |
|
1349 SELECT * from polls WHERE question LIKE 'Who%' |
|
1350 AND (pub_date = '2005-05-02' OR pub_date = '2005-05-06') |
|
1351 |
|
1352 Lookup functions can mix the use of ``Q`` objects and keyword arguments. All |
|
1353 arguments provided to a lookup function (be they keyword arguments or ``Q`` |
|
1354 objects) are "AND"ed together. However, if a ``Q`` object is provided, it must |
|
1355 precede the definition of any keyword arguments. For example:: |
|
1356 |
|
1357 Poll.objects.get( |
|
1358 Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)), |
|
1359 question__startswith='Who') |
|
1360 |
|
1361 ... would be a valid query, equivalent to the previous example; but:: |
|
1362 |
|
1363 # INVALID QUERY |
|
1364 Poll.objects.get( |
|
1365 question__startswith='Who', |
|
1366 Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6))) |
|
1367 |
|
1368 ... would not be valid. |
|
1369 |
|
1370 See the `OR lookups examples page`_ for more examples. |
|
1371 |
|
1372 .. _OR lookups examples page: http://www.djangoproject.com/documentation/models/or_lookups/ |
|
1373 |
|
1374 Related objects |
|
1375 =============== |
|
1376 |
|
1377 When you define a relationship in a model (i.e., a ``ForeignKey``, |
|
1378 ``OneToOneField``, or ``ManyToManyField``), instances of that model will have |
|
1379 a convenient API to access the related object(s). |
|
1380 |
|
1381 Using the models at the top of this page, for example, an ``Entry`` object ``e`` |
|
1382 can get its associated ``Blog`` object by accessing the ``blog`` attribute: |
|
1383 ``e.blog``. |
|
1384 |
|
1385 (Behind the scenes, this functionality is implemented by Python descriptors_. |
|
1386 This shouldn't really matter to you, but we point it out here for the curious.) |
|
1387 |
|
1388 Django also creates API accessors for the "other" side of the relationship -- |
|
1389 the link from the related model to the model that defines the relationship. |
|
1390 For example, a ``Blog`` object ``b`` has access to a list of all related |
|
1391 ``Entry`` objects via the ``entry_set`` attribute: ``b.entry_set.all()``. |
|
1392 |
|
1393 All examples in this section use the sample ``Blog``, ``Author`` and ``Entry`` |
|
1394 models defined at the top of this page. |
|
1395 |
|
1396 .. _descriptors: http://users.rcn.com/python/download/Descriptor.htm |
|
1397 |
|
1398 One-to-many relationships |
|
1399 ------------------------- |
|
1400 |
|
1401 Forward |
|
1402 ~~~~~~~ |
|
1403 |
|
1404 If a model has a ``ForeignKey``, instances of that model will have access to |
|
1405 the related (foreign) object via a simple attribute of the model. |
|
1406 |
|
1407 Example:: |
|
1408 |
|
1409 e = Entry.objects.get(id=2) |
|
1410 e.blog # Returns the related Blog object. |
|
1411 |
|
1412 You can get and set via a foreign-key attribute. As you may expect, changes to |
|
1413 the foreign key aren't saved to the database until you call ``save()``. |
|
1414 Example:: |
|
1415 |
|
1416 e = Entry.objects.get(id=2) |
|
1417 e.blog = some_blog |
|
1418 e.save() |
|
1419 |
|
1420 If a ``ForeignKey`` field has ``null=True`` set (i.e., it allows ``NULL`` |
|
1421 values), you can assign ``None`` to it. Example:: |
|
1422 |
|
1423 e = Entry.objects.get(id=2) |
|
1424 e.blog = None |
|
1425 e.save() # "UPDATE blog_entry SET blog_id = NULL ...;" |
|
1426 |
|
1427 Forward access to one-to-many relationships is cached the first time the |
|
1428 related object is accessed. Subsequent accesses to the foreign key on the same |
|
1429 object instance are cached. Example:: |
|
1430 |
|
1431 e = Entry.objects.get(id=2) |
|
1432 print e.blog # Hits the database to retrieve the associated Blog. |
|
1433 print e.blog # Doesn't hit the database; uses cached version. |
|
1434 |
|
1435 Note that the ``select_related()`` ``QuerySet`` method recursively prepopulates |
|
1436 the cache of all one-to-many relationships ahead of time. Example:: |
|
1437 |
|
1438 e = Entry.objects.select_related().get(id=2) |
|
1439 print e.blog # Doesn't hit the database; uses cached version. |
|
1440 print e.blog # Doesn't hit the database; uses cached version. |
|
1441 |
|
1442 ``select_related()`` is documented in the "QuerySet methods that return new |
|
1443 QuerySets" section above. |
|
1444 |
|
1445 Backward |
|
1446 ~~~~~~~~ |
|
1447 |
|
1448 If a model has a ``ForeignKey``, instances of the foreign-key model will have |
|
1449 access to a ``Manager`` that returns all instances of the first model. By |
|
1450 default, this ``Manager`` is named ``FOO_set``, where ``FOO`` is the source |
|
1451 model name, lowercased. This ``Manager`` returns ``QuerySets``, which can be |
|
1452 filtered and manipulated as described in the "Retrieving objects" section |
|
1453 above. |
|
1454 |
|
1455 Example:: |
|
1456 |
|
1457 b = Blog.objects.get(id=1) |
|
1458 b.entry_set.all() # Returns all Entry objects related to Blog. |
|
1459 |
|
1460 # b.entry_set is a Manager that returns QuerySets. |
|
1461 b.entry_set.filter(headline__contains='Lennon') |
|
1462 b.entry_set.count() |
|
1463 |
|
1464 You can override the ``FOO_set`` name by setting the ``related_name`` |
|
1465 parameter in the ``ForeignKey()`` definition. For example, if the ``Entry`` |
|
1466 model was altered to ``blog = ForeignKey(Blog, related_name='entries')``, the |
|
1467 above example code would look like this:: |
|
1468 |
|
1469 b = Blog.objects.get(id=1) |
|
1470 b.entries.all() # Returns all Entry objects related to Blog. |
|
1471 |
|
1472 # b.entries is a Manager that returns QuerySets. |
|
1473 b.entries.filter(headline__contains='Lennon') |
|
1474 b.entries.count() |
|
1475 |
|
1476 You cannot access a reverse ``ForeignKey`` ``Manager`` from the class; it must |
|
1477 be accessed from an instance. Example:: |
|
1478 |
|
1479 Blog.entry_set # Raises AttributeError: "Manager must be accessed via instance". |
|
1480 |
|
1481 In addition to the ``QuerySet`` methods defined in "Retrieving objects" above, |
|
1482 the ``ForeignKey`` ``Manager`` has these additional methods: |
|
1483 |
|
1484 * ``add(obj1, obj2, ...)``: Adds the specified model objects to the related |
|
1485 object set. |
|
1486 |
|
1487 Example:: |
|
1488 |
|
1489 b = Blog.objects.get(id=1) |
|
1490 e = Entry.objects.get(id=234) |
|
1491 b.entry_set.add(e) # Associates Entry e with Blog b. |
|
1492 |
|
1493 * ``create(**kwargs)``: Creates a new object, saves it and puts it in the |
|
1494 related object set. Returns the newly created object. |
|
1495 |
|
1496 Example:: |
|
1497 |
|
1498 b = Blog.objects.get(id=1) |
|
1499 e = b.entry_set.create(headline='Hello', body_text='Hi', pub_date=datetime.date(2005, 1, 1)) |
|
1500 # No need to call e.save() at this point -- it's already been saved. |
|
1501 |
|
1502 This is equivalent to (but much simpler than):: |
|
1503 |
|
1504 b = Blog.objects.get(id=1) |
|
1505 e = Entry(blog=b, headline='Hello', body_text='Hi', pub_date=datetime.date(2005, 1, 1)) |
|
1506 e.save() |
|
1507 |
|
1508 Note that there's no need to specify the keyword argument of the model |
|
1509 that defines the relationship. In the above example, we don't pass the |
|
1510 parameter ``blog`` to ``create()``. Django figures out that the new |
|
1511 ``Entry`` object's ``blog`` field should be set to ``b``. |
|
1512 |
|
1513 * ``remove(obj1, obj2, ...)``: Removes the specified model objects from the |
|
1514 related object set. |
|
1515 |
|
1516 Example:: |
|
1517 |
|
1518 b = Blog.objects.get(id=1) |
|
1519 e = Entry.objects.get(id=234) |
|
1520 b.entry_set.remove(e) # Disassociates Entry e from Blog b. |
|
1521 |
|
1522 In order to prevent database inconsistency, this method only exists on |
|
1523 ``ForeignKey`` objects where ``null=True``. If the related field can't be |
|
1524 set to ``None`` (``NULL``), then an object can't be removed from a |
|
1525 relation without being added to another. In the above example, removing |
|
1526 ``e`` from ``b.entry_set()`` is equivalent to doing ``e.blog = None``, |
|
1527 and because the ``blog`` ``ForeignKey`` doesn't have ``null=True``, this |
|
1528 is invalid. |
|
1529 |
|
1530 * ``clear()``: Removes all objects from the related object set. |
|
1531 |
|
1532 Example:: |
|
1533 |
|
1534 b = Blog.objects.get(id=1) |
|
1535 b.entry_set.clear() |
|
1536 |
|
1537 Note this doesn't delete the related objects -- it just disassociates |
|
1538 them. |
|
1539 |
|
1540 Just like ``remove()``, ``clear()`` is only available on ``ForeignKey``s |
|
1541 where ``null=True``. |
|
1542 |
|
1543 To assign the members of a related set in one fell swoop, just assign to it |
|
1544 from any iterable object. Example:: |
|
1545 |
|
1546 b = Blog.objects.get(id=1) |
|
1547 b.entry_set = [e1, e2] |
|
1548 |
|
1549 If the ``clear()`` method is available, any pre-existing objects will be |
|
1550 removed from the ``entry_set`` before all objects in the iterable (in this |
|
1551 case, a list) are added to the set. If the ``clear()`` method is *not* |
|
1552 available, all objects in the iterable will be added without removing any |
|
1553 existing elements. |
|
1554 |
|
1555 Each "reverse" operation described in this section has an immediate effect on |
|
1556 the database. Every addition, creation and deletion is immediately and |
|
1557 automatically saved to the database. |
|
1558 |
|
1559 Many-to-many relationships |
|
1560 -------------------------- |
|
1561 |
|
1562 Both ends of a many-to-many relationship get automatic API access to the other |
|
1563 end. The API works just as a "backward" one-to-many relationship. See Backward_ |
|
1564 above. |
|
1565 |
|
1566 The only difference is in the attribute naming: The model that defines the |
|
1567 ``ManyToManyField`` uses the attribute name of that field itself, whereas the |
|
1568 "reverse" model uses the lowercased model name of the original model, plus |
|
1569 ``'_set'`` (just like reverse one-to-many relationships). |
|
1570 |
|
1571 An example makes this easier to understand:: |
|
1572 |
|
1573 e = Entry.objects.get(id=3) |
|
1574 e.authors.all() # Returns all Author objects for this Entry. |
|
1575 e.authors.count() |
|
1576 e.authors.filter(name__contains='John') |
|
1577 |
|
1578 a = Author.objects.get(id=5) |
|
1579 a.entry_set.all() # Returns all Entry objects for this Author. |
|
1580 |
|
1581 Like ``ForeignKey``, ``ManyToManyField`` can specify ``related_name``. In the |
|
1582 above example, if the ``ManyToManyField`` in ``Entry`` had specified |
|
1583 ``related_name='entries'``, then each ``Author`` instance would have an |
|
1584 ``entries`` attribute instead of ``entry_set``. |
|
1585 |
|
1586 One-to-one relationships |
|
1587 ------------------------ |
|
1588 |
|
1589 The semantics of one-to-one relationships will be changing soon, so we don't |
|
1590 recommend you use them. |
|
1591 |
|
1592 How are the backward relationships possible? |
|
1593 -------------------------------------------- |
|
1594 |
|
1595 Other object-relational mappers require you to define relationships on both |
|
1596 sides. The Django developers believe this is a violation of the DRY (Don't |
|
1597 Repeat Yourself) principle, so Django only requires you to define the |
|
1598 relationship on one end. |
|
1599 |
|
1600 But how is this possible, given that a model class doesn't know which other |
|
1601 model classes are related to it until those other model classes are loaded? |
|
1602 |
|
1603 The answer lies in the ``INSTALLED_APPS`` setting. The first time any model is |
|
1604 loaded, Django iterates over every model in ``INSTALLED_APPS`` and creates the |
|
1605 backward relationships in memory as needed. Essentially, one of the functions |
|
1606 of ``INSTALLED_APPS`` is to tell Django the entire model domain. |
|
1607 |
|
1608 Queries over related objects |
|
1609 ---------------------------- |
|
1610 |
|
1611 Queries involving related objects follow the same rules as queries involving |
|
1612 normal value fields. When specifying the the value for a query to match, you |
|
1613 may use either an object instance itself, or the primary key value for the |
|
1614 object. |
|
1615 |
|
1616 For example, if you have a Blog object ``b`` with ``id=5``, the following |
|
1617 three queries would be identical:: |
|
1618 |
|
1619 Entry.objects.filter(blog=b) # Query using object instance |
|
1620 Entry.objects.filter(blog=b.id) # Query using id from instance |
|
1621 Entry.objects.filter(blog=5) # Query using id directly |
|
1622 |
|
1623 Deleting objects |
|
1624 ================ |
|
1625 |
|
1626 The delete method, conveniently, is named ``delete()``. This method immediately |
|
1627 deletes the object and has no return value. Example:: |
|
1628 |
|
1629 e.delete() |
|
1630 |
|
1631 You can also delete objects in bulk. Every ``QuerySet`` has a ``delete()`` |
|
1632 method, which deletes all members of that ``QuerySet``. |
|
1633 |
|
1634 For example, this deletes all ``Entry`` objects with a ``pub_date`` year of |
|
1635 2005:: |
|
1636 |
|
1637 Entry.objects.filter(pub_date__year=2005).delete() |
|
1638 |
|
1639 When Django deletes an object, it emulates the behavior of the SQL |
|
1640 constraint ``ON DELETE CASCADE`` -- in other words, any objects which |
|
1641 had foreign keys pointing at the object to be deleted will be deleted |
|
1642 along with it. For example:: |
|
1643 |
|
1644 b = Blog.objects.get(pk=1) |
|
1645 # This will delete the Blog and all of its Entry objects. |
|
1646 b.delete() |
|
1647 |
|
1648 Note that ``delete()`` is the only ``QuerySet`` method that is not exposed on a |
|
1649 ``Manager`` itself. This is a safety mechanism to prevent you from accidentally |
|
1650 requesting ``Entry.objects.delete()``, and deleting *all* the entries. If you |
|
1651 *do* want to delete all the objects, then you have to explicitly request a |
|
1652 complete query set:: |
|
1653 |
|
1654 Entry.objects.all().delete() |
|
1655 |
|
1656 Extra instance methods |
|
1657 ====================== |
|
1658 |
|
1659 In addition to ``save()``, ``delete()``, a model object might get any or all |
|
1660 of the following methods: |
|
1661 |
|
1662 get_FOO_display() |
|
1663 ----------------- |
|
1664 |
|
1665 For every field that has ``choices`` set, the object will have a |
|
1666 ``get_FOO_display()`` method, where ``FOO`` is the name of the field. This |
|
1667 method returns the "human-readable" value of the field. For example, in the |
|
1668 following model:: |
|
1669 |
|
1670 GENDER_CHOICES = ( |
|
1671 ('M', 'Male'), |
|
1672 ('F', 'Female'), |
|
1673 ) |
|
1674 class Person(models.Model): |
|
1675 name = models.CharField(maxlength=20) |
|
1676 gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) |
|
1677 |
|
1678 ...each ``Person`` instance will have a ``get_gender_display()`` method. Example:: |
|
1679 |
|
1680 >>> p = Person(name='John', gender='M') |
|
1681 >>> p.save() |
|
1682 >>> p.gender |
|
1683 'M' |
|
1684 >>> p.get_gender_display() |
|
1685 'Male' |
|
1686 |
|
1687 get_next_by_FOO(\**kwargs) and get_previous_by_FOO(\**kwargs) |
|
1688 ------------------------------------------------------------- |
|
1689 |
|
1690 For every ``DateField`` and ``DateTimeField`` that does not have ``null=True``, |
|
1691 the object will have ``get_next_by_FOO()`` and ``get_previous_by_FOO()`` |
|
1692 methods, where ``FOO`` is the name of the field. This returns the next and |
|
1693 previous object with respect to the date field, raising the appropriate |
|
1694 ``DoesNotExist`` exception when appropriate. |
|
1695 |
|
1696 Both methods accept optional keyword arguments, which should be in the format |
|
1697 described in `Field lookups`_ above. |
|
1698 |
|
1699 Note that in the case of identical date values, these methods will use the ID |
|
1700 as a fallback check. This guarantees that no records are skipped or duplicated. |
|
1701 For a full example, see the `lookup API sample model`_. |
|
1702 |
|
1703 .. _lookup API sample model: http://www.djangoproject.com/documentation/models/lookup/ |
|
1704 |
|
1705 get_FOO_filename() |
|
1706 ------------------ |
|
1707 |
|
1708 For every ``FileField``, the object will have a ``get_FOO_filename()`` method, |
|
1709 where ``FOO`` is the name of the field. This returns the full filesystem path |
|
1710 to the file, according to your ``MEDIA_ROOT`` setting. |
|
1711 |
|
1712 Note that ``ImageField`` is technically a subclass of ``FileField``, so every |
|
1713 model with an ``ImageField`` will also get this method. |
|
1714 |
|
1715 get_FOO_url() |
|
1716 ------------- |
|
1717 |
|
1718 For every ``FileField``, the object will have a ``get_FOO_url()`` method, |
|
1719 where ``FOO`` is the name of the field. This returns the full URL to the file, |
|
1720 according to your ``MEDIA_URL`` setting. If the value is blank, this method |
|
1721 returns an empty string. |
|
1722 |
|
1723 get_FOO_size() |
|
1724 -------------- |
|
1725 |
|
1726 For every ``FileField``, the object will have a ``get_FOO_size()`` method, |
|
1727 where ``FOO`` is the name of the field. This returns the size of the file, in |
|
1728 bytes. (Behind the scenes, it uses ``os.path.getsize``.) |
|
1729 |
|
1730 save_FOO_file(filename, raw_contents) |
|
1731 ------------------------------------- |
|
1732 |
|
1733 For every ``FileField``, the object will have a ``save_FOO_file()`` method, |
|
1734 where ``FOO`` is the name of the field. This saves the given file to the |
|
1735 filesystem, using the given filename. If a file with the given filename already |
|
1736 exists, Django adds an underscore to the end of the filename (but before the |
|
1737 extension) until the filename is available. |
|
1738 |
|
1739 get_FOO_height() and get_FOO_width() |
|
1740 ------------------------------------ |
|
1741 |
|
1742 For every ``ImageField``, the object will have ``get_FOO_height()`` and |
|
1743 ``get_FOO_width()`` methods, where ``FOO`` is the name of the field. This |
|
1744 returns the height (or width) of the image, as an integer, in pixels. |
|
1745 |
|
1746 Shortcuts |
|
1747 ========= |
|
1748 |
|
1749 As you develop views, you will discover a number of common idioms in the |
|
1750 way you use the database API. Django encodes some of these idioms as |
|
1751 shortcuts that can be used to simplify the process of writing views. |
|
1752 |
|
1753 get_object_or_404() |
|
1754 ------------------- |
|
1755 |
|
1756 One common idiom to use ``get()`` and raise ``Http404`` if the |
|
1757 object doesn't exist. This idiom is captured by ``get_object_or_404()``. |
|
1758 This function takes a Django model as its first argument and an |
|
1759 arbitrary number of keyword arguments, which it passes to the manager's |
|
1760 ``get()`` function. It raises ``Http404`` if the object doesn't |
|
1761 exist. For example:: |
|
1762 |
|
1763 # Get the Entry with a primary key of 3 |
|
1764 e = get_object_or_404(Entry, pk=3) |
|
1765 |
|
1766 When you provide a model to this shortcut function, the default manager |
|
1767 is used to execute the underlying ``get()`` query. If you don't want to |
|
1768 use the default manager, or you want to search a list of related objects, |
|
1769 you can provide ``get_object_or_404()`` with a manager object, instead. |
|
1770 For example:: |
|
1771 |
|
1772 # Get the author of blog instance `e` with a name of 'Fred' |
|
1773 a = get_object_or_404(e.authors, name='Fred') |
|
1774 |
|
1775 # Use a custom manager 'recent_entries' in the search for an |
|
1776 # entry with a primary key of 3 |
|
1777 e = get_object_or_404(Entry.recent_entries, pk=3) |
|
1778 |
|
1779 get_list_or_404() |
|
1780 ----------------- |
|
1781 |
|
1782 ``get_list_or_404`` behaves the same was as ``get_object_or_404()`` |
|
1783 -- except the it uses using ``filter()`` instead of ``get()``. It raises |
|
1784 ``Http404`` if the list is empty. |
|
1785 |
|
1786 Falling back to raw SQL |
|
1787 ======================= |
|
1788 |
|
1789 If you find yourself needing to write an SQL query that is too complex for |
|
1790 Django's database-mapper to handle, you can fall back into raw-SQL statement |
|
1791 mode. |
|
1792 |
|
1793 The preferred way to do this is by giving your model custom methods or custom |
|
1794 manager methods that execute queries. Although there's nothing in Django that |
|
1795 *requires* database queries to live in the model layer, this approach keeps all |
|
1796 your data-access logic in one place, which is smart from an code-organization |
|
1797 standpoint. For instructions, see `Executing custom SQL`_. |
|
1798 |
|
1799 Finally, it's important to note that the Django database layer is merely an |
|
1800 interface to your database. You can access your database via other tools, |
|
1801 programming languages or database frameworks; there's nothing Django-specific |
|
1802 about your database. |
|
1803 |
|
1804 .. _Executing custom SQL: ../model_api/#executing-custom-sql |