Skip to content

Commit

Permalink
[soc2009/multidb] Correct the handling of raw and defered fields with…
Browse files Browse the repository at this point in the history
… multi-db.

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11926 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Dec 21, 2009
1 parent 8da7538 commit 4424a8d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
1 change: 1 addition & 0 deletions django/db/models/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def _update(self, values, **kwargs):
return self.get_query_set()._update(values, **kwargs)

def raw(self, query, params=None, *args, **kwargs):
kwargs["using"] = self.db
return RawQuerySet(model=self.model, query=query, params=params, *args, **kwargs)

class ManagerDescriptor(object):
Expand Down
8 changes: 6 additions & 2 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,11 @@ class RawQuerySet(object):
Provides an iterator which converts the results of raw SQL queries into
annotated model instances.
"""
def __init__(self, query, model=None, query_obj=None, params=None, translations=None):
def __init__(self, query, model=None, query_obj=None, params=None,
translations=None, using=None):
self.model = model
self.query = query_obj or sql.RawQuery(sql=query, connection=connection, params=params)
self.using = using
self.query = query_obj or sql.RawQuery(sql=query, connection=connections[using], params=params)
self.params = params or ()
self.translations = translations or {}

Expand Down Expand Up @@ -1230,6 +1232,8 @@ def transform_results(self, values):

for field, value in annotations:
setattr(instance, field, value)

instance._state.db = self.using

return instance

Expand Down
2 changes: 1 addition & 1 deletion django/db/models/query_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __get__(self, instance, owner):
cls = self.model_ref()
data = instance.__dict__
if data.get(self.field_name, self) is self:
data[self.field_name] = cls._base_manager.filter(pk=instance.pk).values_list(self.field_name, flat=True).get()
data[self.field_name] = cls._base_manager.filter(pk=instance.pk).values_list(self.field_name, flat=True).using(instance._state.db).get()
return data[self.field_name]

def __set__(self, instance, value):
Expand Down
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.utils.tree import Node
from django.utils.datastructures import SortedDict
from django.utils.encoding import force_unicode
from django.db import connection, connections, DEFAULT_DB_ALIAS
from django.db import connections, DEFAULT_DB_ALIAS
from django.db.models import signals
from django.db.models.fields import FieldDoesNotExist
from django.db.models.query_utils import select_related_descend, InvalidQuery
Expand Down
7 changes: 7 additions & 0 deletions tests/regressiontests/multiple_database/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ def test_ordering(self):

self.assertEquals(learn.get_next_by_published().title, "Dive into Python")
self.assertEquals(dive.get_previous_by_published().title, "Learning Python")

def test_raw(self):
"test the raw() method across databases"
dive = Book.objects.using('other').create(title="Dive into Python",
published=datetime.date(2009, 5, 4))
val = Book.objects.db_manager("other").raw('SELECT id FROM "multiple_database_book"')
self.assertEqual(map(lambda o: o.pk, val), [dive.pk])


class UserProfileTestCase(TestCase):
Expand Down

0 comments on commit 4424a8d

Please sign in to comment.