From a81813fb4aec2e25850780c3846ad9743bf460ed Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 15 Mar 2008 01:59:06 +0000 Subject: [PATCH] queryset-refactor: Fixed the "in" lookup type when using tuples. Accidentally broken in r7170. Fixed #6772. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7244 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/fields/related.py | 3 ++- django/db/models/query_utils.py | 8 ++++++++ django/db/models/sql/where.py | 5 +++-- tests/modeltests/or_lookups/models.py | 2 ++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 818c8a9d7c..1bc9b87ef8 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -2,6 +2,7 @@ from django.db import connection, transaction from django.db.models import signals, get_model from django.db.models.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class from django.db.models.related import RelatedObject +from django.db.models.query_utils import QueryWrapper from django.utils.text import capfirst from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _ from django.utils.functional import curry @@ -138,7 +139,7 @@ class RelatedField(object): if hasattr(value, 'as_sql'): sql, params = value.as_sql() - return ('(%s)' % sql), params + return QueryWrapper(('(%s)' % sql), params) if lookup_type == 'exact': return [pk_trace(value)] if lookup_type == 'in': diff --git a/django/db/models/query_utils.py b/django/db/models/query_utils.py index 426811be89..003628ad94 100644 --- a/django/db/models/query_utils.py +++ b/django/db/models/query_utils.py @@ -15,6 +15,14 @@ class EmptyResultSet(Exception): """ pass +class QueryWrapper(object): + """ + A type that indicates the contents are an SQL fragment and the associate + parameters. Can be used to pass opaque data to a where-clause, for example. + """ + def __init__(self, sql, params): + self.data = sql, params + class Q(tree.Node): """ Encapsulates filters as objects that can then be combined logically (using diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index e1c0a88121..f0d2786a49 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -6,6 +6,7 @@ import datetime from django.utils import tree from django.db import connection from django.db.models.fields import Field +from django.db.models.query_utils import QueryWrapper from datastructures import EmptyResultSet, FullResultSet # Connection types @@ -110,8 +111,8 @@ class WhereNode(tree.Node): params = field.get_db_prep_lookup(lookup_type, value) else: params = Field().get_db_prep_lookup(lookup_type, value) - if isinstance(params, tuple): - extra, params = params + if isinstance(params, QueryWrapper): + extra, params = params.data else: extra = '' diff --git a/tests/modeltests/or_lookups/models.py b/tests/modeltests/or_lookups/models.py index b3500f27e7..38339c0685 100644 --- a/tests/modeltests/or_lookups/models.py +++ b/tests/modeltests/or_lookups/models.py @@ -70,6 +70,8 @@ __test__ = {'API_TESTS':""" # You could also use "in" to accomplish the same as above. >>> Article.objects.filter(pk__in=[1,2,3]) [, , ] +>>> Article.objects.filter(pk__in=(1,2,3)) +[, , ] >>> Article.objects.filter(pk__in=[1,2,3,4]) [, , ]