Ticket #6065: sql_notin.patch

File sql_notin.patch, 1.7 KB (added by RichardH <Richard.House@…>, 8 years ago)

Patch for implementing NOTIN

  • django/db/models/fields/__init__.py

     
    211211        "Returns field's value prepared for database lookup."
    212212        if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'search'):
    213213            return [value]
    214         elif lookup_type in ('range', 'in'):
     214        elif lookup_type in ('range', 'in', 'notin'):
    215215            return value
    216216        elif lookup_type in ('contains', 'icontains'):
    217217            return ["%%%s%%" % prep_for_like_query(value)]
  • django/db/models/query.py

     
    2121# The list of valid query types
    2222QUERY_TERMS = (
    2323    'exact', 'iexact', 'contains', 'icontains',
    24     'gt', 'gte', 'lt', 'lte', 'in',
     24    'gt', 'gte', 'lt', 'lte', 'in', 'notin',
    2525    'startswith', 'istartswith', 'endswith', 'iendswith',
    2626    'range', 'year', 'month', 'day', 'isnull', 'search',
    2727    'regex', 'iregex',
     
    806806            return '%s IN (%s)' % (field_sql, in_string)
    807807        else:
    808808            raise EmptyResultSet
     809    elif lookup_type == 'notin':
     810        in_string = ','.join(['%s' for id in value])
     811        if in_string:
     812            return '%s NOT IN (%s)' % (field_sql, in_string)
     813        else:
     814            raise EmptyResultSet
    809815    elif lookup_type in ('range', 'year'):
    810816        return '%s BETWEEN %%s AND %%s' % field_sql
    811817    elif lookup_type in ('month', 'day'):
Back to Top