Django

Code

Ticket #2737 (closed: fixed)

Opened 2 years ago

Last modified 3 weeks ago

DB API and Model Interface don't agree on meaning of None

Reported by: wardi-django@excess.org Assigned to: adrian
Component: Database wrapper Version: SVN
Keywords: exclude filter qs-rf-fixed Cc: mir@noris.de, hv@tbz-pariv.de, django@jensdiemer.de
Triage Stage: Accepted Has patch: 1
Needs documentation: 1 Needs tests: 1
Patch needs improvement: 0

Description

When the value of a field in a model is None it corresponds to a NULL item in the database.

However, when using filter() or exclude() you can't pass my_field=None eg:

SomeModel.objects.filter(my_field=None)

does nothing, while

SomeModel.objects.exclude(my_field=None)

generates invalid SQL.

I know the preferred way to test for NULL is with __isnull, but the code above is what I tried first. I would like it if my_field=None is treated as equivalent to my_field__isnull=True (a naive patch is attached) but if not, then at least there should be a useful error raised when you try something like my_field=None.

Attachments

isnull.patch (483 bytes) - added by wardi-django@excess.org on 09/15/06 09:03:00.
[patch] adds support for my_field=None in filter() and exclude()
none-isnull.patch (0.5 kB) - added by Collin Grady <cgrady@the-magi.us> on 04/30/07 17:11:23.

Change History

09/15/06 09:03:00 changed by wardi-django@excess.org

  • attachment isnull.patch added.

[patch] adds support for my_field=None in filter() and exclude()

09/15/06 12:00:43 changed by django@poelzi.org

I agree, i run into this sometimes, because for me filter(bla=None) simply feels like "bla IS NULL". +1 from me

09/15/06 16:38:28 changed by anonymous

  • cc set to mir@noris.de.

I had assumed that, different from the opinions abovem, filter(xxx=value) should mean the same as where xxx=value for all possible values. And in case of None, this means where xxx=NULL, yielding an empty result set. Current treatment of None values is really a riddle!

09/19/06 11:23:40 changed by wardi-django@excess.org

I don't really like SQL oddities like "xxx=NULL always gives no results" leaking through django's python-like database abstraction layer.

Another sql oddity that I've run into is:

query_set.filter(name="foo") | query_set.exclude(name="foo") != query_set

if name can be NULL.. but I can live with that.

09/26/06 08:43:09 changed by mtredinnick

  • type changed from defect to enhancement.

10/13/06 21:48:05 changed by russellm

  • status changed from new to closed.
  • resolution set to fixed.

(In [3902]) Fixes #2737 -- Added code to allow None as a query value for exact queries, raising an error otherwise. exact=None is interpreted as the SQL 'value = NULL'. This fixes some minor problems with queries on unsaved objects with related object sets, and stops queries with a value of None being outright ignored (even if they reference an unknown attribute).

04/30/07 17:11:02 changed by Collin Grady <cgrady@the-magi.us>

  • status changed from closed to reopened.
  • has_patch set to 1.
  • resolution deleted.

.filter(foo=None) is actually sending "= None" not "IS NULL" - attaching patch to correct

04/30/07 17:11:23 changed by Collin Grady <cgrady@the-magi.us>

  • attachment none-isnull.patch added.

04/30/07 22:40:19 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to wontfix.

The current behaviour is intentional and the behaviour is documented.

See this thread for the justification. It's a weird corner-case, but doing an exact match against NULL won't give sensible results in any case, as mentioned in the above thread.

11/16/07 07:53:11 changed by gwilson

  • status changed from closed to reopened.
  • resolution deleted.
  • stage changed from Unreviewed to Accepted.

Re-opening this, since in this thread, we have Adrian +1, Malcolm +0, Russell +0, and me +1.

11/19/07 16:22:27 changed by SmileyChris

  • needs_docs set to 1.
  • needs_tests set to 1.

11/19/07 18:17:33 changed by mtredinnick

  • keywords changed from exclude filter None to exclude filter Noneqs-rf.

11/19/07 18:47:57 changed by SmileyChris

  • keywords changed from exclude filter Noneqs-rf to exclude filter qs-rf.

11/20/07 08:24:08 changed by Thomas Güttler <hv@tbz-pariv.de>

  • cc changed from mir@noris.de to mir@noris.de, hv@tbz-pariv.de.

11/23/07 13:17:58 changed by Thomas Güttler (Home) < >

+1 SomeModel?.objects.filter(my_field=None) should behave like my_fileisnull=True

11/29/07 20:29:26 changed by mtredinnick

(In [6760]) queryset-refactor: Interpret qs.filter(foo=None) to be the same as qs.filter(fooisnull=True). Refs #2737.

11/29/07 20:30:21 changed by mtredinnick

  • keywords changed from exclude filter qs-rf to exclude filter qs-rf-fixed.

03/13/08 08:13:31 changed by jedie

  • cc changed from mir@noris.de, hv@tbz-pariv.de to mir@noris.de, hv@tbz-pariv.de, django@jensdiemer.de.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658


Add/Change #2737 (DB API and Model Interface don't agree on meaning of None)




Change Properties
Action