I have a model where each Blah can come from one or more Sources.  
i.e. I have:

class Blah(models.Model):
     ...
     sources = models.ManyToManyField(Source)


The query I want to perform is to find all the Blahs that come from  
sources 1 or 2 but not sources 3 or 4.

My first attempt was:

        Blah.objects.filter(sources__id__in=[1, 2]).exclude(sources__id__in= 
[3, 4])

but that just returns all Blahs (the filter(sources__id__in=[1, 2])  
is definitely wrong)

The SQL I want to run is something like:

        SELECT blah_id
        FROM myapp_blah_sources
        WHERE source_id IN (1, 2)
        EXCEPT
        SELECT blah_id
        FROM myapp_blah_sources
        WHERE source_id in (3, 4);

and this seems to work. Note it's directly referencing the m2m relation.

What's the best way to express that SQL query in the database API?

Note that the lists [1, 2] and [3, 4] are actually coming from a form  
and may be empty, in which case the WHERE clause could be eliminated  
on that side of the EXCEPT.


Thanks


James


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to