Friday, July 26, 2013

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

> So, if one of the fields can be Null, then *neither*:
>
> queryset.filter(field=value)
>
> queryset.exclude(field=value)
>
> will match a record where it's Null?

As I understand it, this is correct - it's certainly the way SQL was designed.

> In that case, is there a better - more reliable - way than using both .filter() and .exclude() with the same terms to split a queryset into all those items that match a filter, and all those that don't?

As well as doing the queries above, you can get the nulls by doing

queryset.filter(field__isnull=True)

and then it's just a matter of deciding which of your two sets the
nulls should be included in (probably with a logical OR). There are a
number of other approaches as well, but somewhere we'll still have to
decide what the meaning of null. My bias is towards making that
explicit. The other obvious approach is to find queryset.all() and
then the (null-less) set queryset.filter(field=value) and work with
them.

--
steve
http://stevemcconville.com/

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate