Ticket #11864: 11864.diff

File 11864.diff, 1.7 KB (added by Derek Willis, 8 years ago)
  • docs/topics/db/models.txt

    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 
    302302        manufacturer = models.ForeignKey(Manufacturer)
    303303        # ...
     305You can follow the relationship backwards from ``Manufacturer`` to ``Car``, too.
     306Using the model definitions above, an instance of ``Manufacturer`` will have
     307access to  a ``Manager`` that returns its related ``Car`` instances. By default,
     308this ``Manager`` is named ``FOO_set``, where ``FOO`` is the lower-cased named of
     309the related model::
     311    >>> manufacturer = Manufacturer.objects.get(id=1)
     312    >>> manufacturer.car_set.all()
     314Calling the ``all()`` method on the ``Manager`` returns all of the related ``Car``
     315instances, and because the ``Manager`` returns a ``QuerySet``, you also can
     316filter the results.
     318.. seealso::
     320    See the `Following relationships backward example`_ for more details.
     322.. _Following relationships backward example: http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects
    305324You can also create :ref:`recursive relationships <recursive-relationships>` (an
    306325object with a many-to-one relationship to itself) and :ref:`relationships to
    307326models not yet defined <lazy-relationships>`; see :ref:`the model field
Back to Top