Options.default_related_name
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 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 replaced respectively by the name of the application the model is in, and the name of the model, both lowercased. See the paragraph on related names for abstract models.
Deprecated since version 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.objects.get(bar=bar) >>> # You should use default_related_name "bars". >>> Foo.objects.get(bars=bar)
Please login to continue.