Author: Alex
Date: 2009-12-16 01:01:56 -0600 (Wed, 16 Dec 2009)
New Revision: 11873

Modified:
   django/branches/soc2009/multidb/TODO
   django/branches/soc2009/multidb/django/db/models/fields/related.py
   
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
Log:
[soc2009/multidb] Fixed a problem with m2m descriptors not sticking to the 
right database.  Patch from Russell Keith-Magee.

Modified: django/branches/soc2009/multidb/TODO
===================================================================
--- django/branches/soc2009/multidb/TODO        2009-12-16 02:33:33 UTC (rev 
11872)
+++ django/branches/soc2009/multidb/TODO        2009-12-16 07:01:56 UTC (rev 
11873)
@@ -5,6 +5,9 @@
 ~~~~~~~~~~~~~~~~~
 
  * Modify the admin interface to support multiple databases (doh).
+    - Document how it is done
+    - Modify m2m widgets to stick to the same database as parent
+    - Modify inlines to stick to same database as parent
 
 Optional for v1.2
 ~~~~~~~~~~~~~~~~~

Modified: django/branches/soc2009/multidb/django/db/models/fields/related.py
===================================================================
--- django/branches/soc2009/multidb/django/db/models/fields/related.py  
2009-12-16 02:33:33 UTC (rev 11872)
+++ django/branches/soc2009/multidb/django/db/models/fields/related.py  
2009-12-16 07:01:56 UTC (rev 11873)
@@ -439,7 +439,7 @@
                 raise ValueError("%r instance needs to have a primary key 
value before a many-to-many relationship can be used." % 
instance.__class__.__name__)
 
         def get_query_set(self):
-            return 
superclass.get_query_set(self)._next_is_sticky().filter(**(self.core_filters))
+            return 
superclass.get_query_set(self).using(self.instance._state.db)._next_is_sticky().filter(**(self.core_filters))
 
         # If the ManyToMany relation has an intermediary model,
         # the add and remove methods do not exist.
@@ -709,7 +709,7 @@
 
 class ForeignKey(RelatedField, Field):
     """Foreign Key (type determined by related field)"""
-    
+
     empty_strings_allowed = False
     def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
         try:
@@ -809,7 +809,7 @@
 
 class OneToOneField(ForeignKey):
     """One-to-one relationship
-    
+
     A OneToOneField is essentially the same as a ForeignKey, with the exception
     that always carries a "unique" constraint with it and the reverse relation
     always returns the object pointed to (since there will only ever be one),
@@ -869,7 +869,7 @@
 
 class ManyToManyField(RelatedField, Field):
     """Many-to-many relationship"""
-    
+
     def __init__(self, to, **kwargs):
         try:
             assert not to._meta.abstract, "%s cannot define a relation with 
abstract class %s" % (self.__class__.__name__, to._meta.object_name)
@@ -1032,7 +1032,10 @@
         setattr(instance, self.attname, data)
 
     def formfield(self, **kwargs):
-        defaults = {'form_class': forms.ModelMultipleChoiceField, 'queryset': 
self.rel.to._default_manager.complex_filter(self.rel.limit_choices_to)}
+        defaults = {
+            'form_class': forms.ModelMultipleChoiceField,
+            'queryset': 
self.rel.to._default_manager.complex_filter(self.rel.limit_choices_to)
+        }
         defaults.update(kwargs)
         # If initial is passed in, it's a list of related objects, but the
         # MultipleChoiceField takes a list of IDs.

Modified: 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
===================================================================
--- 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-12-16 02:33:33 UTC (rev 11872)
+++ 
django/branches/soc2009/multidb/tests/regressiontests/multiple_database/tests.py
    2009-12-16 07:01:56 UTC (rev 11873)
@@ -160,6 +160,17 @@
         
self.assertEquals(list(Book.objects.using('other').filter(authors__name='Mark 
Pilgrim').values_list('title', flat=True)),
                           [u'Dive into Python'])
 
+        # Reget the objects to clear caches
+        dive = Book.objects.using('other').get(title="Dive into Python")
+        mark = Author.objects.using('other').get(name="Mark Pilgrim")
+
+        # Retrive related object by descriptor. Related objects should be 
database-baound
+        self.assertEquals(list(dive.authors.all().values_list('name', 
flat=True)),
+                          [u'Mark Pilgrim'])
+
+        self.assertEquals(list(mark.book_set.all().values_list('title', 
flat=True)),
+                          [u'Dive into Python'])
+
     def test_m2m_forward_operations(self):
         "M2M forward manipulations are all constrained to a single DB"
         # Create a book and author on the other database
@@ -286,6 +297,16 @@
         
self.assertEquals(list(Book.objects.using('other').filter(favourite_of__name='Mark
 Pilgrim').values_list('title', flat=True)),
                           [u'Dive into Python'])
 
+        # Reget the objects to clear caches
+        dive = Book.objects.using('other').get(title="Dive into Python")
+        mark = Author.objects.using('other').get(name="Mark Pilgrim")
+
+        # Retrive related object by descriptor. Related objects should be 
database-baound
+        self.assertEquals(list(dive.favourite_of.all().values_list('name', 
flat=True)),
+                          [u'Mark Pilgrim'])
+
+        self.assertEquals(mark.favourite_book.title, u'Dive into Python')
+
     def test_foreign_key_reverse_operations(self):
         "FK reverse manipulations are all constrained to a single DB"
         dive = Book.objects.using('other').create(title="Dive into Python",

--

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


Reply via email to