Monday, July 29, 2013

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



On Sunday, July 28, 2013 10:44:49 AM UTC+3, Daniele Procida wrote:
On Sat, Jul 27, 2013, Steve McConville <mcconvil...@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.

You can do this with a subquery in Django. non_red_things = queryset.exclude(pk__in=red_things). If this performs well is a different thing.

I think that in SQL one can use WHERE (original_condition) is not true; which will match both unknown (null comparison's result) and false in the original condition.

 - Anssi

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