Friday, August 29, 2014

related_query_name - what is it?

Django doc says:

 ForeignKey.related_query_name
New in Django 1.6.
The name to use for the reverse filter name from the target model. Defaults to the value of related_name if it is set, otherwise it defaults to the name of the model:

# Declare the ForeignKey with related_query_name
class Tag(models.Model):
    article = models.ForeignKey(Article, related_name="tags", related_query_name="tag")
    name = models.CharField(max_length=255)

# That's now the name of the reverse filter
article_instance.filter(tag__name="important")

However, an object model doesn't have filter() method. I am trying to use related_query_name to filter a model M with a condition on the related objects R. The problem is that result includes instances of M which don't have related objects in R. But I would like to first understand what related_query_name is. Could someone explain? Thanks,

Petr

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/086400af-967e-45ea-a413-4ca4fcaf17a9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment