On Fri, 2009-03-06 at 00:11 -0800, Daniel Hepper wrote:
> > > 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.)
Yes, you're completely correct. My apologies (and thanks for not just
believe me)! I was being lazy and didn't think it through. The second
query tries to find a single article objects that simultaneously has pk
values of 1, 2 and 3, as you notice in your SQL. Unsurprisingly, that
doesn't exist.
>
> 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])
That can be done already. Although, as I mentioned in my first reply to
the original poster, the question is ambiguous.
I also realised last night that my first reply on this topic also had a
stupid mistake caused by me trying to answer both questions at once.
I've already shown how to get all objects that are in precisely those
categories and no others. Making that easier (e.g. two straight filters
or something similar) is actually fairly hard internally, since the API
shouldn't suck (I've spent a couple of hours thinking about it and
poking at the code, so I'm not just guessing here).
However, find things that are simultaneously in all those categories can
be done without all the annotation nonsense I posted. Simply
Publication.objects.filter(article=1).filter(article=2).filter(article=3)
It's a short loop to build up such a set dynamically. The difference
between that (3 filter calls) and you Q-object version is described in
the documentation:
http://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships
Regards,
Malcolm
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---