[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
This commit is contained in:
Ramiro Morales 2011-01-14 23:30:17 +00:00
parent eb9ebb1931
commit 74a61e35b8
2 changed files with 14 additions and 4 deletions

View File

@ -612,7 +612,7 @@ class Model(object):
for related in self._meta.get_all_related_many_to_many_objects(): for related in self._meta.get_all_related_many_to_many_objects():
if related.field.rel.through: 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 opts = related.field.rel.through._meta
reverse_field_name = related.field.m2m_reverse_field_name() reverse_field_name = related.field.m2m_reverse_field_name()
nullable = opts.get_field(reverse_field_name).null nullable = opts.get_field(reverse_field_name).null
@ -622,7 +622,7 @@ class Model(object):
for f in self._meta.many_to_many: for f in self._meta.many_to_many:
if f.rel.through: 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 opts = f.rel.through._meta
field_name = f.m2m_field_name() field_name = f.m2m_field_name()
nullable = opts.get_field(field_name).null nullable = opts.get_field(field_name).null

View File

@ -1739,7 +1739,7 @@ class ModelMetaRouter(object):
if not hasattr(model, '_meta'): if not hasattr(model, '_meta'):
raise ValueError raise ValueError
class RouterM2MThroughTestCase(TestCase): class RouterModelArgumentTestCase(TestCase):
multi_db = True multi_db = True
def setUp(self): def setUp(self):
@ -1749,7 +1749,7 @@ class RouterM2MThroughTestCase(TestCase):
def tearDown(self): def tearDown(self):
router.routers = self.old_routers router.routers = self.old_routers
def test_m2m_through(self): def test_m2m_collection(self):
b = Book.objects.create(title="Pro Django", b = Book.objects.create(title="Pro Django",
published=datetime.date(2008, 12, 16)) published=datetime.date(2008, 12, 16))
@ -1760,3 +1760,13 @@ class RouterM2MThroughTestCase(TestCase):
b.authors.remove(p) b.authors.remove(p)
# test clear # test clear
b.authors.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()