Sunday, July 28, 2013

Re: .filter() and .exclude() don't add up

On Sat, Jul 27, 2013, Steve McConville <mcconville.steve@gmail.com> wrote:

>Perhaps I'm not sure exactly what you mean by "more general", but I
>was recommending something like
>
>red_things = queryset.filter(Q(color="red"))
>non_red_things = queryset.filter(~Q(color="red") | Q(color__isnull=True)
>
>This will produce SQL like
>
>SELECT * FROM queryset WHERE color IS 'red';
>SELECT * FROM queryset WHERE color IS NOT 'red' OR color IS NULL;
>
>The set non_red_things will be the complement of red_things.

I understood that part. But by "more general" I mean one that will work for any case, without having to know where the Nulls might be.

So given queryset A, and its subset queryset B, we can place B against A and obtain its complement.

Or to put it another way: give me all the items in A that are not in B.

Daniele



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment