Thank you for the detailed response.

Russell Keith-Magee wrote:
> > But if I AND them (here is the problem/question), nothing...
> > ...
> > So, is this a bug? Or am I doing something wrong?
>
>
> It's not a bug - it's a slight misunderstanding on your part on how
> filtering works.

Yep, I realized that even more after trying this by hand.


> The correct way to compose your query would be to do the AND on the query
> set, rather than in the filter;
>
> QueryTerm.objects.filter(classification=4) & QueryTerm.objects.filter
> (classification=6)
>
> This gets two distinct sets of objects, and then determines the elements
> that exist in both sets (in this case, just queryterm 1). Unfortunately, the
> QuerySet intersection/union operations are the subject of a known bug
> (ticket #1801), so while the syntax exists, this might not actually work in
> your case.

Yep, it doesn't work. Same result. :-(


> So, other ways to solve the problem:
> 1) Get the two QuerySets and perform the intersection yourself using Python
> intersection operators. This requires two SQL operations and doesn't exploit
> the capabilities of having your data in a relational database, so the code
> will not be as efficient.

At this point, I only care that it works. :-) But this is not going to
be "clean Django", alas.


> 2) Roll your own SQL; what you want can be achieved if you perform 2 joins
> with the classification m2m table (rather than the single join that Django
> uses) and add the right WHERE clauses. This isn't as pretty, but should be
> faster at runtime.

Alas, I am not a SQL guru. But I think this will still be easier than
trying to shoehorn the current Django implemention into this.

> You _might_ be able to acheive option 2 without dropping to raw SQL if you
> write a custom Q object to force a second join; however, this approach is
> not for beginners. Search the archives of this mailing list for others that
> have attempted this approach (there have been a couple in the last 2 months)
> and start spelunking through django/db/models/query.py if you feel enthused.

Thanks again. I'll do some more digging. Ticket 1801 looks like a good
start.


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to