Author: Alex
Date: 2009-06-18 12:31:36 -0500 (Thu, 18 Jun 2009)
New Revision: 11073

Modified:
   django/branches/soc2009/multidb/django/db/models/query.py
   django/branches/soc2009/multidb/django/db/utils.py
   
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
Log:
[soc2009/multidb] Ensure that when a QuerySet is given a Query object in its 
construct that we correct the detect the connection that is being used

Modified: django/branches/soc2009/multidb/django/db/models/query.py
===================================================================
--- django/branches/soc2009/multidb/django/db/models/query.py   2009-06-18 
17:05:59 UTC (rev 11072)
+++ django/branches/soc2009/multidb/django/db/models/query.py   2009-06-18 
17:31:36 UTC (rev 11073)
@@ -38,8 +38,7 @@
         self._result_cache = None
         self._iter = None
         self._sticky_filter = False
-        self._using = DEFAULT_DB_ALIAS # this will be wrong if a custom Query
-                                       # is provided with a non default 
connection
+        self._using = connections.alias_for_connection(self.query.connection)
 
     ########################
     # PYTHON MAGIC METHODS #

Modified: django/branches/soc2009/multidb/django/db/utils.py
===================================================================
--- django/branches/soc2009/multidb/django/db/utils.py  2009-06-18 17:05:59 UTC 
(rev 11072)
+++ django/branches/soc2009/multidb/django/db/utils.py  2009-06-18 17:31:36 UTC 
(rev 11073)
@@ -68,3 +68,13 @@
 
     def all(self):
         return [self[alias] for alias in self]
+
+    def alias_for_connection(self, connection):
+        """
+        Returns the alias for the given connection object.
+        """
+        for alias in self:
+            conn_settings = self.databases[alias]
+            if conn_settings == connection.settings_dict:
+                return alias
+        return None

Modified: 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
===================================================================
--- 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-06-18 17:05:59 UTC (rev 11072)
+++ 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-06-18 17:31:36 UTC (rev 11073)
@@ -6,7 +6,7 @@
 
 from models import Book
 
-class DatabaseSettingTestCase(TestCase):
+class ConnectionHandlerTestCase(TestCase):
     def setUp(self):
         settings.DATABASES['__test_db'] = {
             'DATABASE_ENGINE': 'sqlite3',
@@ -20,6 +20,11 @@
         connections['default'].cursor()
         connections['__test_db'].cursor()
 
+    def test_alias_for_connection(self):
+        for db in connections:
+            self.assertEqual(db, 
connections.alias_for_connection(connections[db]))
+
+
 class QueryTestCase(TestCase):
     def test_basic_queries(self):
         for db in connections:


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to