Fixed #10913 -- Documented how related_name affects QuerySet filtering

Thanks neithere for the suggestion.
This commit is contained in:
Tim Graham 2013-10-02 12:12:53 -04:00
parent e1b7723817
commit 75bb6ba966
2 changed files with 13 additions and 10 deletions

View File

@ -1078,10 +1078,11 @@ define the details of how the relation works.
.. attribute:: ForeignKey.related_name .. attribute:: ForeignKey.related_name
The name to use for the relation from the related object back to this one. The name to use for the relation from the related object back to this one.
See the :ref:`related objects documentation <backwards-related-objects>` for It's also the default value for :attr:`related_query_name` (the name to use
a full explanation and example. Note that you must set this value for the reverse filter name from the target model). See the :ref:`related
when defining relations on :ref:`abstract models objects documentation <backwards-related-objects>` for a full explanation
<abstract-base-classes>`; and when you do so and example. Note that you must set this value when defining relations on
:ref:`abstract models <abstract-base-classes>`; and when you do so
:ref:`some special syntax <abstract-related-name>` is available. :ref:`some special syntax <abstract-related-name>` is available.
If you'd prefer Django not to create a backwards relation, set If you'd prefer Django not to create a backwards relation, set

View File

@ -1124,8 +1124,9 @@ Example::
>>> b.entry_set.filter(headline__contains='Lennon') >>> b.entry_set.filter(headline__contains='Lennon')
>>> b.entry_set.count() >>> b.entry_set.count()
You can override the ``FOO_set`` name by setting the ``related_name`` You can override the ``FOO_set`` name by setting the
parameter in the ``ForeignKey()`` definition. For example, if the ``Entry`` :attr:`~django.db.models.ForeignKey.related_name` parameter in the
:class:`~django.db.models.ForeignKey` definition. For example, if the ``Entry``
model was altered to ``blog = ForeignKey(Blog, related_name='entries')``, the model was altered to ``blog = ForeignKey(Blog, related_name='entries')``, the
above example code would look like this:: above example code would look like this::
@ -1224,10 +1225,11 @@ An example makes this easier to understand::
a.entry_set.all() # Returns all Entry objects for this Author. a.entry_set.all() # Returns all Entry objects for this Author.
Like :class:`~django.db.models.ForeignKey`, Like :class:`~django.db.models.ForeignKey`,
:class:`~django.db.models.ManyToManyField` can specify ``related_name``. In the :class:`~django.db.models.ManyToManyField` can specify
above example, if the :class:`~django.db.models.ManyToManyField` in ``Entry`` :attr:`~django.db.models.ManyToManyField.related_name`. In the above example,
had specified ``related_name='entries'``, then each ``Author`` instance would if the :class:`~django.db.models.ManyToManyField` in ``Entry`` had specified
have an ``entries`` attribute instead of ``entry_set``. ``related_name='entries'``, then each ``Author`` instance would have an
``entries`` attribute instead of ``entry_set``.
One-to-one relationships One-to-one relationships
------------------------ ------------------------