Django

Code

Changeset 6760

Show
Ignore:
Timestamp:
11/29/07 20:29:25 (9 months ago)
Author:
mtredinnick
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/sql/query.py

    r6755 r6760  
    680680            lookup_type = parts.pop() 
    681681 
    682         # Interpret '__exact=None' as the sql '= NULL'; otherwise, reject all 
     682        # Interpret '__exact=None' as the sql 'is NULL'; otherwise, reject all 
    683683        # uses of None as a query value. 
    684         # FIXME: Weren't we going to change this so that '__exact=None' was the 
    685         # same as '__isnull=True'? Need to check the conclusion of the mailing 
    686         # list thread. 
    687         if value is None and lookup_type != 'exact': 
    688             raise ValueError("Cannot use None as a query value") 
     684        if value is None: 
     685            if lookup_type != 'exact': 
     686                raise ValueError("Cannot use None as a query value") 
     687            lookup_type = 'isnull' 
     688            value = True 
    689689        elif callable(value): 
    690690            value = value() 
     
    709709                col = join[LHS_JOIN_COL] 
    710710 
    711         if (lookup_type == 'isnull' and value is True): 
     711        if lookup_type == 'isnull' and value is True and (len(join_list) > 1 or 
     712                len(join_list[0]) > 1): 
    712713            # If the comparison is against NULL, we need to use a left outer 
    713714            # join when connecting to the previous model. We make that 
  • django/branches/queryset-refactor/docs/db-api.txt

    r6603 r6760  
    11381138 
    11391139    SELECT ... WHERE id = 14; 
    1140     SELECT ... WHERE id = NULL; 
     1140    SELECT ... WHERE id IS NULL; 
     1141 
     1142**New in Django development version:** The semantics of ``id__exact=None`` have 
     1143changed in the development version. Previously, it was (intentionally) 
     1144converted to ``WHERE id = NULL`` at the SQL level, which would never match 
     1145anything. It has now been changed to behave the same as ``id__isnull=True``. 
    11411146 
    11421147iexact 
     
    13671372 
    13681373    SELECT ... WHERE pub_date IS NULL; 
    1369  
    1370 .. admonition:: ``__isnull=True`` vs ``__exact=None`` 
    1371  
    1372     There is an important difference between ``__isnull=True`` and 
    1373     ``__exact=None``. ``__exact=None`` will *always* return an empty result 
    1374     set, because SQL requires that no value is equal to ``NULL``. 
    1375     ``__isnull`` determines if the field is currently holding the value 
    1376     of ``NULL`` without performing a comparison. 
    13771374 
    13781375search 
  • django/branches/queryset-refactor/tests/modeltests/many_to_one_null/models.py

    r5876 r6760  
    8181[<Article: Third>] 
    8282 
     83# We can achieve the same thing by filtering for the case where the reporter is 
     84# None. 
     85>>> Article.objects.filter(reporter=None) 
     86[<Article: Third>] 
     87 
    8388# Set the reporter for the Third article 
    8489>>> r.article_set.add(a3) 
  • django/branches/queryset-refactor/tests/regressiontests/null_queries/models.py

    r6730 r6760  
    2525>>> c2.save() 
    2626 
    27 # Exact query with value None returns nothing (=NULL in sql) 
     27# Exact query with value None returns nothing ("is NULL" in sql, but every 'id' 
     28# field has a value). 
    2829>>> Choice.objects.filter(id__exact=None) 
    2930[] 
     31 
     32Excluding the previous result returns everything. 
     33>>> Choice.objects.exclude(id=None).order_by('id') 
     34[<Choice: Choice: Because. in poll Q: Why? >, <Choice: Choice: Why Not? in poll Q: Why? >] 
    3035 
    3136# Valid query, but fails because foo isn't a keyword