Code

Opened 9 years ago

Closed 9 years ago

Last modified 7 years ago

#166 closed enhancement (fixed)

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

Reported by: rmunn@… Owned by: adrian
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: UI/UX:

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]

Attachments (0)

Change History (3)

comment:1 Changed 9 years ago by rmunn@…

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 Changed 9 years ago by jacob

  • Summary changed from [patch] Add field__in to the lookup types in the DB API to [patch] Add "in" to the lookup types in the DB API

comment:3 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.