mirror of
https://github.com/django/django.git
synced 2025-07-04 17:59:13 +00:00
[soc2009/multidb] Correct the handling of raw and defered fields with multi-db.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/multidb@11926 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
8da7538b18
commit
4424a8d3d8
@ -200,6 +200,7 @@ class Manager(object):
|
||||
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):
|
||||
|
@ -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 {}
|
||||
|
||||
@ -1230,6 +1232,8 @@ class RawQuerySet(object):
|
||||
|
||||
for field, value in annotations:
|
||||
setattr(instance, field, value)
|
||||
|
||||
instance._state.db = self.using
|
||||
|
||||
return instance
|
||||
|
||||
|
@ -187,7 +187,7 @@ class DeferredAttribute(object):
|
||||
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):
|
||||
|
@ -11,7 +11,7 @@ from django.utils.copycompat import deepcopy
|
||||
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
|
||||
|
@ -619,6 +619,13 @@ class QueryTestCase(TestCase):
|
||||
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user