Opened 19 years ago

Closed 19 years ago

Last modified 17 years ago

#166 closed enhancement (fixed)

[patch] Add "in" to the lookup types in the DB API

Reported by: rmunn@… Owned by: Adrian Holovaty
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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]

Change History (3)

comment:1 by rmunn@…, 19 years ago

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.

comment:2 by Jacob, 19 years ago

Summary: [patch] Add field__in to the lookup types in the DB API[patch] Add "in" to the lookup types in the DB API

comment:3 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

(In [318]) Fixed #166 -- Added an 'in' lookup type to the database API

Note: See TracTickets for help on using tickets.
Back to Top