On 11/27/06, halmcelroy <[EMAIL PROTECTED]> wrote:
>
> In SQL this would translate as:
> SELECT * FROM A WHERE ID NOT IN (SELECT ID FROM B)
...
> for.eg. A.objects.filter().exclude(this__not_in=[.. some list of
> objects that I want to exclude])
>
> Can anyone throw some light on how to use the models API to get the
> above done?

Ok; step one, drop the redundant filter() and the double negatives.
The purpose of exclude() is to provide the NOT clause; so, what you
want is:

A.objects.exclude(id__in=[...])

i.e., get a list of all A's where A.id is not in [..list of ids..].

The empty filter() in your example is not required; it's a no-op. The
exclude clause takes all the query arguments, composes them as a
query, then wraps them in a NOT.

Step Two; getting the subselect. Generally speaking, subselects such
as the one you want can be achieved using two queries:

b = B.objects.filter(...query conditions...)
A.objects.exclude(id__in=[obj.id for obj in b])

There are some ways that you could compress this into a single query
using the extra() clause, but these are not as pretty and involve some
hand generated SQL snippets.

Yours,
Russ Magee %-)

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