From 74a61e35b82c8823ec73da5eb23c97e6e1724f06 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Fri, 14 Jan 2011 23:30:17 +0000 Subject: [PATCH] [1.2.X] Fixed #14948 -- Fixed a couple of cases where invalid model classes were passed to the database router when collecting reverse foreign key and many to many relationships. Thanks shell_dweller for the report and Harm Geerts for the patch. This also enhances tests added in r15186. Code in SVN trunk doesn't suffer from this problem because it was refactored in r14507. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15207 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 4 ++-- tests/regressiontests/multiple_database/tests.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 60b98ec739..462483d058 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -612,7 +612,7 @@ class Model(object): for related in self._meta.get_all_related_many_to_many_objects(): if related.field.rel.through: - db = router.db_for_write(related.field.rel.through.__class__, instance=self) + db = router.db_for_write(related.field.rel.through, instance=self) opts = related.field.rel.through._meta reverse_field_name = related.field.m2m_reverse_field_name() nullable = opts.get_field(reverse_field_name).null @@ -622,7 +622,7 @@ class Model(object): for f in self._meta.many_to_many: if f.rel.through: - db = router.db_for_write(f.rel.through.__class__, instance=self) + db = router.db_for_write(f.rel.through, instance=self) opts = f.rel.through._meta field_name = f.m2m_field_name() nullable = opts.get_field(field_name).null diff --git a/tests/regressiontests/multiple_database/tests.py b/tests/regressiontests/multiple_database/tests.py index c0bcedf0aa..052396bd75 100644 --- a/tests/regressiontests/multiple_database/tests.py +++ b/tests/regressiontests/multiple_database/tests.py @@ -1739,7 +1739,7 @@ class ModelMetaRouter(object): if not hasattr(model, '_meta'): raise ValueError -class RouterM2MThroughTestCase(TestCase): +class RouterModelArgumentTestCase(TestCase): multi_db = True def setUp(self): @@ -1749,7 +1749,7 @@ class RouterM2MThroughTestCase(TestCase): def tearDown(self): router.routers = self.old_routers - def test_m2m_through(self): + def test_m2m_collection(self): b = Book.objects.create(title="Pro Django", published=datetime.date(2008, 12, 16)) @@ -1760,3 +1760,13 @@ class RouterM2MThroughTestCase(TestCase): b.authors.remove(p) # test clear b.authors.clear() + # test setattr + b.authors = [p] + # Test M2M collection (_collect_sub_objects() in Django <= 1.2.X) + b.delete() + + def test_foreignkey_collection(self): + person = Person.objects.create(name='Bob') + pet = Pet.objects.create(owner=person, name='Wart') + # Test related FK collection (_collect_sub_objects() in Django <= 1.2.X) + person.delete()