From 4d6c6d05964e8ec73159768de333ce210ac08e4d Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 6 Jun 2009 19:38:29 +0000 Subject: [PATCH] [soc2009/multidb] Fixed the usage of the connection during Query construction, and defer it until actual SQL construction. In practice this means the GROUP BY optimization we do will be correctly applied based on the connection the Query is executed against, as oppossed to the on the QuerySet is targeting at the time the GROUP BY items are added git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@10934 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- TODO.TXT | 1 - django/db/models/fields/__init__.py | 1 + django/db/models/sql/query.py | 8 +++----- tests/regressiontests/multiple_database/tests.py | 7 +++++++ 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/TODO.TXT b/TODO.TXT index 058079770a..bd439de3e0 100644 --- a/TODO.TXT +++ b/TODO.TXT @@ -49,7 +49,6 @@ that need to be done. I'm trying to be as granular as possible. phase. This involves changing the following methods: * ``Query.add_aggregate`` - * ``Query.set_group_by`` * ``DateQuery.add_date_select`` * ``Field.get_db_prep_lookup`` * ``DateField.get_db_prep_value`` diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index f8a94e41ec..d5c7490781 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -204,6 +204,7 @@ class Field(object): sql, params = value._as_sql() return QueryWrapper(('(%s)' % sql), params) + if lookup_type in ('regex', 'iregex', 'month', 'day', 'week_day', 'search'): return [value] elif lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte'): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 2033bee371..6acd649621 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -878,6 +878,9 @@ class BaseQuery(object): qn = self.quote_name_unless_alias result, params = [], [] if self.group_by is not None: + if len(self.model._meta.fields) == len(self.group_by) and \ + self.connection.features.allows_group_by_pk: + self.group_by = [(self.model._meta.db_table, self.model._meta.pk.column)] group_by = self.group_by or [] extra_selects = [] @@ -2099,11 +2102,6 @@ class BaseQuery(object): will be made automatically. """ self.group_by = [] - if self.connection.features.allows_group_by_pk: - if len(self.select) == len(self.model._meta.fields): - self.group_by.append((self.model._meta.db_table, - self.model._meta.pk.column)) - return for sel in self.select: self.group_by.append(sel) diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index a94232edb0..9578a26706 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -49,3 +49,10 @@ class QueryTestCase(TestCase): dive = Book.objects.using(db).get(title__icontains="dive") self.assertEqual(dive.title, "Dive into Python") + + dive = Book.objects.using(db).get(title__iexact="dive INTO python") + self.assertEqual(dive.title, "Dive into Python") + + pro = Book.objects.using(db).get(published__year=2008) + self.assertEqual(pro.title, "Pro Django") + self.assertEqual(pro.published, datetime.date(2008, 12, 16))