Fixed #26230 -- Made default_related_name affect related_query_name.

This commit is contained in:
chenesan 2016-02-24 15:10:09 +08:00 committed by Tim Graham
parent 5fb9756eba
commit b84f5ab4ec
8 changed files with 97 additions and 8 deletions

View file

@ -1333,8 +1333,9 @@ The possible values for :attr:`~ForeignKey.on_delete` are found in
.. attribute:: ForeignKey.related_query_name
The name to use for the reverse filter name from the target model.
Defaults to the value of :attr:`related_name` if it is set, otherwise it
The name to use for the reverse filter name from the target model. It
defaults to the value of :attr:`related_name` or
:attr:`~django.db.models.Options.default_related_name` if set, otherwise it
defaults to the name of the model::
# Declare the ForeignKey with related_query_name

View file

@ -103,6 +103,8 @@ Django quotes column and table names behind the scenes.
The name that will be used by default for the relation from a related object
back to this one. The default is ``<model_name>_set``.
This option also sets :attr:`~ForeignKey.related_query_name`.
As the reverse name for a field should be unique, be careful if you intend
to subclass your model. To work around name collisions, part of the name
should contain ``'%(app_label)s'`` and ``'%(model_name)s'``, which are
@ -110,6 +112,30 @@ Django quotes column and table names behind the scenes.
and the name of the model, both lowercased. See the paragraph on
:ref:`related names for abstract models <abstract-related-name>`.
.. deprecated:: 1.10
This attribute now affects ``related_query_name``. The old query lookup
name is deprecated::
from django.db import models
class Foo(models.Model):
pass
class Bar(models.Model):
foo = models.ForeignKey(Foo)
class Meta:
default_related_name = 'bars'
::
>>> bar = Bar.objects.get(pk=1)
>>> # Using model name "bar" as lookup string is deprecated.
>>> Foo.object.get(bar=bar)
>>> # You should use default_related_name "bars".
>>> Foo.object.get(bars=bar)
``get_latest_by``
-----------------