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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/20170216065000.GR23772%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Digital signature

Reply via email to