On Wed, Feb 15, 2017 at 12:48:21PM -0800, Luvpreet Singh wrote:
> What is the difference between realted_name and related_query_name ??
One is used as the name of the related manager on the remote side, the
other is used to traverse the relationship in queryset filters from
the remote side.
Let me try to illustrate that with an example.
class Reporter(models.Model):
name = models.TextField()
class Article(models.Model):
reporter = models.ForeignKey(Reporter, related_name="articles",
related_query_name="articles_written")
title = models.TextField()
So, when you have an instance of the Reporter model, you'd use the
related_name, which is “articles” here, to get a list of articles:
r = Reporter.objects.last()
r.articles.all()
However, if you want to get all reporters, who have written articles
with titles beginning with the letter “T”, you'd use the
related_query_name in the queryset filter:
Reporter.objects.filter(articles_written__title__startswith="T")
Does this make it more clear?
Cheers,
Michal