mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -2,6 +2,7 @@ from django.db import connection, transaction | |||||||
| from django.db.models import signals, get_model | 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.fields import AutoField, Field, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, get_ul_class | ||||||
| from django.db.models.related import RelatedObject | from django.db.models.related import RelatedObject | ||||||
|  | from django.db.models.query_utils import QueryWrapper | ||||||
| from django.utils.text import capfirst | from django.utils.text import capfirst | ||||||
| from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _ | from django.utils.translation import ugettext_lazy, string_concat, ungettext, ugettext as _ | ||||||
| from django.utils.functional import curry | from django.utils.functional import curry | ||||||
| @@ -138,7 +139,7 @@ class RelatedField(object): | |||||||
|  |  | ||||||
|         if hasattr(value, 'as_sql'): |         if hasattr(value, 'as_sql'): | ||||||
|             sql, params = value.as_sql() |             sql, params = value.as_sql() | ||||||
|             return ('(%s)' % sql), params |             return QueryWrapper(('(%s)' % sql), params) | ||||||
|         if lookup_type == 'exact': |         if lookup_type == 'exact': | ||||||
|             return [pk_trace(value)] |             return [pk_trace(value)] | ||||||
|         if lookup_type == 'in': |         if lookup_type == 'in': | ||||||
|   | |||||||
| @@ -15,6 +15,14 @@ class EmptyResultSet(Exception): | |||||||
|     """ |     """ | ||||||
|     pass |     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): | class Q(tree.Node): | ||||||
|     """ |     """ | ||||||
|     Encapsulates filters as objects that can then be combined logically (using |     Encapsulates filters as objects that can then be combined logically (using | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ import datetime | |||||||
| from django.utils import tree | from django.utils import tree | ||||||
| from django.db import connection | from django.db import connection | ||||||
| from django.db.models.fields import Field | from django.db.models.fields import Field | ||||||
|  | from django.db.models.query_utils import QueryWrapper | ||||||
| from datastructures import EmptyResultSet, FullResultSet | from datastructures import EmptyResultSet, FullResultSet | ||||||
|  |  | ||||||
| # Connection types | # Connection types | ||||||
| @@ -110,8 +111,8 @@ class WhereNode(tree.Node): | |||||||
|             params = field.get_db_prep_lookup(lookup_type, value) |             params = field.get_db_prep_lookup(lookup_type, value) | ||||||
|         else: |         else: | ||||||
|             params = Field().get_db_prep_lookup(lookup_type, value) |             params = Field().get_db_prep_lookup(lookup_type, value) | ||||||
|         if isinstance(params, tuple): |         if isinstance(params, QueryWrapper): | ||||||
|             extra, params = params |             extra, params = params.data | ||||||
|         else: |         else: | ||||||
|             extra = '' |             extra = '' | ||||||
|  |  | ||||||
|   | |||||||
| @@ -70,6 +70,8 @@ __test__ = {'API_TESTS':""" | |||||||
| # You could also use "in" to accomplish the same as above. | # 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: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] | [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] | ||||||
|  | >>> Article.objects.filter(pk__in=(1,2,3)) | ||||||
|  | [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] | ||||||
|  |  | ||||||
| >>> Article.objects.filter(pk__in=[1,2,3,4]) | >>> Article.objects.filter(pk__in=[1,2,3,4]) | ||||||
| [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] | [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>] | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user