Django

Code

Changeset 3902

Show
Ignore:
Timestamp:
10/13/06 21:48:05 (2 years ago)
Author:
russellm
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r3826 r3902  
    708708 
    709709    for kwarg, value in kwarg_items: 
    710         if value is not None: 
    711             path = kwarg.split(LOOKUP_SEPARATOR) 
    712             # Extract the last elements of the kwarg. 
    713             # The very-last is the lookup_type (equals, like, etc). 
    714             # The second-last is the table column on which the lookup_type is 
    715             # to be performed. If this name is 'pk', it will be substituted with 
    716             # the name of the primary key. 
    717             # If there is only one part, or the last part is not a query 
    718             # term, assume that the query is an __exact 
    719             lookup_type = path.pop() 
    720             if lookup_type == 'pk': 
    721                 lookup_type = 'exact' 
    722                 path.append(None) 
    723             elif len(path) == 0 or lookup_type not in QUERY_TERMS: 
    724                 path.append(lookup_type) 
    725                 lookup_type = 'exact' 
    726  
    727             if len(path) < 1: 
    728                 raise TypeError, "Cannot parse keyword query %r" % kwarg 
    729  
    730             joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None) 
    731             joins.update(joins2) 
    732             where.extend(where2) 
    733             params.extend(params2) 
     710        path = kwarg.split(LOOKUP_SEPARATOR) 
     711        # Extract the last elements of the kwarg. 
     712        # The very-last is the lookup_type (equals, like, etc). 
     713        # The second-last is the table column on which the lookup_type is 
     714        # to be performed. If this name is 'pk', it will be substituted with 
     715        # the name of the primary key. 
     716        # If there is only one part, or the last part is not a query 
     717        # term, assume that the query is an __exact 
     718        lookup_type = path.pop() 
     719        if lookup_type == 'pk': 
     720            lookup_type = 'exact' 
     721            path.append(None) 
     722        elif len(path) == 0 or lookup_type not in QUERY_TERMS: 
     723            path.append(lookup_type) 
     724            lookup_type = 'exact' 
     725 
     726        if len(path) < 1: 
     727            raise TypeError, "Cannot parse keyword query %r" % kwarg 
     728         
     729        if value is None: 
     730            # Interpret '__exact=None' as the sql '= NULL'; otherwise, reject 
     731            # all uses of None as a query value. 
     732            if lookup_type != 'exact': 
     733                raise ValueError, "Cannot use None as a query value" 
     734 
     735        joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None) 
     736        joins.update(joins2) 
     737        where.extend(where2) 
     738        params.extend(params2) 
    734739    return joins, where, params 
    735740 
  • django/trunk/docs/db-api.txt

    r3826 r3902  
    877877~~~~~ 
    878878 
    879 Exact match. 
    880  
    881 Example:: 
     879Exact match. If the value provided for comparison is ``None``, it will  
     880be interpreted as an SQL ``NULL`` (See isnull_ for more details).   
     881 
     882Examples:: 
    882883 
    883884    Entry.objects.get(id__exact=14) 
    884  
    885 SQL equivalent:: 
     885    Entry.objects.get(id__exact=None) 
     886 
     887SQL equivalents:: 
    886888 
    887889    SELECT ... WHERE id = 14; 
     890    SELECT ... WHERE id = NULL; 
    888891 
    889892iexact 
     
    11041107~~~~~~ 
    11051108 
    1106 ``NULL`` or ``IS NOT NULL`` match. Takes either ``True`` or ``False``, which 
    1107 correspond to ``IS NULL`` and ``IS NOT NULL``, respectively. 
     1109Takes either ``True`` or ``False``, which correspond to SQL queries of  
     1110``IS NULL`` and ``IS NOT NULL``, respectively. 
    11081111 
    11091112Example:: 
     
    11141117 
    11151118    SELECT ... WHERE pub_date IS NULL; 
     1119 
     1120.. admonition:: ``__isnull=True`` vs ``__exact=None`` 
     1121 
     1122    There is an important difference between ``__isnull=True`` and  
     1123    ``__exact=None``. ``__exact=None`` will *always* return an empty result 
     1124    set, because SQL requires that no value is equal to ``NULL``.  
     1125    ``__isnull`` determines if the field is currently holding the value  
     1126    of ``NULL`` without performing a comparison. 
    11161127 
    11171128search