Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.2.X] Fixed #14948 -- Fixed a couple of cases where invalid model c…

…lasses 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
commit 74a61e35b82c8823ec73da5eb23c97e6e1724f06 1 parent eb9ebb1
Ramiro Morales ramiro authored
4 django/db/models/base.py
View
@@ -612,7 +612,7 @@ def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
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 @@ def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
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
14 tests/regressiontests/multiple_database/tests.py
View
@@ -1739,7 +1739,7 @@ def db_for_write(self, model, **hints):
if not hasattr(model, '_meta'):
raise ValueError
-class RouterM2MThroughTestCase(TestCase):
+class RouterModelArgumentTestCase(TestCase):
multi_db = True
def setUp(self):
@@ -1749,7 +1749,7 @@ def setUp(self):
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 @@ def test_m2m_through(self):
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()
Please sign in to comment.
Something went wrong with that request. Please try again.