related_query_name - what is it?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

related_query_name - what is it?

Petr Glotov
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: related_query_name - what is it?

Petr Glotov
Correction: first sentence of the question should read:
However, a model instance doesn't have filter() method. 

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/25528fa6-8d01-44a3-b8f7-1230f68f96d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: related_query_name - what is it?

Simon Charette
The documentation is wrong, it should be:

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

I'll commit a documentation admonition and give you credit.

Thanks for spotting this!

Le vendredi 29 août 2014 22:27:38 UTC-4, Petr Glotov a écrit :
Correction: first sentence of the question should read:
However, a model instance doesn't have filter() method. 

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/8ec56a2f-2619-4683-b506-45f62b8d7ad1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: related_query_name - what is it?

Petr Glotov
In reply to this post by Petr Glotov
No problem.

I think I also found how to deal with M instances which are not related to R (R instances have foreign key to M). I tried M.filter(related_query_name_from_R__isnull=False) and it seems to work.

--
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 [hidden email].
To post to this group, send email to [hidden email].
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/7881889c-c9df-4793-bfcb-7b488fbca7fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.