From 0a8787879f8b94ad4802372379f3b7e506fa9515 Mon Sep 17 00:00:00 2001
From: Derek Willis <dwillis@gmail.com>
Date: Thu, 23 Sep 2010 23:14:41 -0400
Subject: [PATCH] added backward relationship example to model topic
---
docs/topics/db/models.txt | 19 +++++++++++++++++++
1 files changed, 19 insertions(+), 0 deletions(-)
diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt
index 0ff34ea..a38dd44 100644
a
|
b
|
For example, if a ``Car`` model has a ``Manufacturer`` -- that is, a
|
302 | 302 | manufacturer = models.ForeignKey(Manufacturer) |
303 | 303 | # ... |
304 | 304 | |
| 305 | You can follow the relationship backwards from ``Manufacturer`` to ``Car``, too. |
| 306 | Using the model definitions above, an instance of ``Manufacturer`` will have |
| 307 | access to a ``Manager`` that returns its related ``Car`` instances. By default, |
| 308 | this ``Manager`` is named ``FOO_set``, where ``FOO`` is the lower-cased named of |
| 309 | the related model:: |
| 310 | |
| 311 | >>> manufacturer = Manufacturer.objects.get(id=1) |
| 312 | >>> manufacturer.car_set.all() |
| 313 | |
| 314 | Calling the ``all()`` method on the ``Manager`` returns all of the related ``Car`` |
| 315 | instances, and because the ``Manager`` returns a ``QuerySet``, you also can |
| 316 | filter the results. |
| 317 | |
| 318 | .. seealso:: |
| 319 | |
| 320 | See the `Following relationships backward example`_ for more details. |
| 321 | |
| 322 | .. _Following relationships backward example: http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects |
| 323 | |
305 | 324 | You can also create :ref:`recursive relationships <recursive-relationships>` (an |
306 | 325 | object with a many-to-one relationship to itself) and :ref:`relationships to |
307 | 326 | models not yet defined <lazy-relationships>`; see :ref:`the model field |