From 284948b9cf275b792369405747c1e112a0b94a48 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Tue, 26 Jul 2005 16:53:58 +0000 Subject: [PATCH] Fixed #166 -- Added an 'in' lookup type to the database API git-svn-id: http://code.djangoproject.com/svn/django/trunk@318 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/meta.py | 6 ++++-- docs/db-api.txt | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/django/core/meta.py b/django/core/meta.py index 51cdee2b63..5780432ee4 100644 --- a/django/core/meta.py +++ b/django/core/meta.py @@ -1020,7 +1020,9 @@ def _get_where_clause(lookup_type, table_prefix, field_name, value): 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': + return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for v in value])) + 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) @@ -1635,7 +1637,7 @@ class Field(object): "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] diff --git a/docs/db-api.txt b/docs/db-api.txt index 548af52a71..91ce2e8ac2 100644 --- a/docs/db-api.txt +++ b/docs/db-api.txt @@ -67,6 +67,8 @@ The DB API supports the following lookup types: lt Less than. lte Less than or equal to. ne Not equal to. + in In a given list: ``polls.get_list(id__in=[1, 3, 4])`` returns + a list of polls whose IDs are either 1, 3 or 4. startswith Case-sensitive starts-with: ``polls.get_list(question_startswith="Would")``. (PostgreSQL only. MySQL doesn't support case-sensitive LIKE statements;