This patch allows one to do field__in=[1,4,7,10]
to select a non-contiguous set of values in a single SELECT statement, which will look like this in SQL: SELECT * FROM table WHERE field IN (1,4,7,10)
Index: django/core/meta.py
===================================================================
--- django/core/meta.py (revision 299)
+++ django/core/meta.py (working copy)
-1018,7 +1020,10 @@
return '%s%s %s %%s' % (table_prefix, field_name, db.OPERATOR_MAPPING[lookup_type])
except KeyError:
pass
- if lookup_type in ('range', 'year'):
+ if lookup_type == 'in':
+ in_clause = ','.join(['%s']*len(value))
+ return '%s%s IN (%s)' % (table_prefix, field_name, in_clause)
+ elif lookup_type in ('range', 'year'):
return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
elif lookup_type in ('month', 'day'):
return "%s = %%s" % db.get_date_extract_sql(lookup_type, table_prefix + field_name)
-1630,7 +1635,7 @@
"Returns field's value prepared for database lookup."
if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'ne', 'month', 'day'):
return [value]
- elif lookup_type == 'range':
+ elif lookup_type in ('range', 'in'):
return value
elif lookup_type == 'year':
return ['%s-01-01' % value, '%s-12-31' % value]
As far as documenting goes:
field__in
expects a sequence. List or tuple, it doesn't matter. It will even use a string (and split it up into characters) if you pass in a string, although that may not be what you'd expected. It just wants a sequence.