> > Book.objects.filter(Q(categories=1), Q(categories=2), Q(categories=3))
>
> Not if you were trying to solve the original poster's question. Your
> query is exactly the same as what he tried to do originally.
I played a bit with the query and just wanted to clarify that it is
not exactly the same. (Note that the Q-objects are "AND"ed, not
"OR"ed).
>>> Publication.objects.filter(article__in=[1,2,3])
[<Publication: Highlights for Children>, <Publication: Science News>,
<Publication: Science Weekly>, <Publication: The Python Journal>,
<Publication: The Python Journal>]
>>> Publication.objects.filter(Q(article=1), Q(article=2), Q(article=3))
[]
(I've used the models from the Many-To-Many-Example _1.)
The second query will always return an empty result. This is the
generated SQL:
{'sql': u'SELECT "bookstore_publication"."id",
"bookstore_publication"."title" FROM "bookstore_publication" INNER
JOIN "bookstore_article_publications" ON ("bookstore_publication"."id"
= "bookstore_article_publications"."publication_id") WHERE
("bookstore_article_publications"."article_id" = 1 AND
"bookstore_article_publications"."article_id" = 2 AND
"bookstore_article_publications"."article_id" = 3 ) ORDER BY
"bookstore_publication"."title" ASC LIMIT 21',
'time': '0.000'}
Thinking about it, this makes perfect sense, as the condition is on
the article, not on the set.
It would be nice to have special lookups for Many-To-Many-Fields or
related sets, which would allow something like
Publication.objects.filter(article_set__exact=[1,2,3])
-- Daniel
1_ http://www.djangoproject.com/documentation/models/many_to_many/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---