Code

Ticket #11864: 11864.diff

File 11864.diff, 1.7 KB (added by dwillis, 4 years ago)
Line 
1From 0a8787879f8b94ad4802372379f3b7e506fa9515 Mon Sep 17 00:00:00 2001
2From: Derek Willis <dwillis@gmail.com>
3Date: Thu, 23 Sep 2010 23:14:41 -0400
4Subject: [PATCH] added backward relationship example to model topic
5
6---
7 docs/topics/db/models.txt |   19 +++++++++++++++++++
8 1 files changed, 19 insertions(+), 0 deletions(-)
9
10diff --git a/docs/topics/db/models.txt b/docs/topics/db/models.txt
11index 0ff34ea..a38dd44 100644
12--- a/docs/topics/db/models.txt
13+++ b/docs/topics/db/models.txt
14@@ -302,6 +302,25 @@ For example, if a ``Car`` model has a ``Manufacturer`` -- that is, a
15         manufacturer = models.ForeignKey(Manufacturer)
16         # ...
17 
18+You can follow the relationship backwards from ``Manufacturer`` to ``Car``, too.
19+Using the model definitions above, an instance of ``Manufacturer`` will have
20+access to  a ``Manager`` that returns its related ``Car`` instances. By default,
21+this ``Manager`` is named ``FOO_set``, where ``FOO`` is the lower-cased named of
22+the related model::
23+
24+    >>> manufacturer = Manufacturer.objects.get(id=1)
25+    >>> manufacturer.car_set.all()
26+   
27+Calling the ``all()`` method on the ``Manager`` returns all of the related ``Car``
28+instances, and because the ``Manager`` returns a ``QuerySet``, you also can
29+filter the results.
30+
31+.. seealso::
32+
33+    See the `Following relationships backward example`_ for more details.
34+   
35+.. _Following relationships backward example: http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects
36+
37 You can also create :ref:`recursive relationships <recursive-relationships>` (an
38 object with a many-to-one relationship to itself) and :ref:`relationships to
39 models not yet defined <lazy-relationships>`; see :ref:`the model field
40--
411.6.5.7
42