Ticket #14948: django-1.2-router_related_accessor-2.patch

File django-1.2-router_related_accessor-2.patch, 3.8 KB (added by Harm Geerts <hgeerts@…>, 13 years ago)

new patch based on changes from #13668 with tests to cover changes in django.db.models.base

  • django/db/models/base.py

     
    612612
    613613        for related in self._meta.get_all_related_many_to_many_objects():
    614614            if related.field.rel.through:
    615                 db = router.db_for_write(related.field.rel.through.__class__, instance=self)
     615                db = router.db_for_write(related.field.rel.through, instance=self)
    616616                opts = related.field.rel.through._meta
    617617                reverse_field_name = related.field.m2m_reverse_field_name()
    618618                nullable = opts.get_field(reverse_field_name).null
     
    622622
    623623        for f in self._meta.many_to_many:
    624624            if f.rel.through:
    625                 db = router.db_for_write(f.rel.through.__class__, instance=self)
     625                db = router.db_for_write(f.rel.through, instance=self)
    626626                opts = f.rel.through._meta
    627627                field_name = f.m2m_field_name()
    628628                nullable = opts.get_field(field_name).null
  • tests/regressiontests/multiple_database/tests.py

     
    16981698    def tearDown(self):
    16991699        router.routers = self.old_routers
    17001700
    1701     def test_attribute_error(self):
     1701    def test_attribute_error_read(self):
    17021702        "Check that the AttributeError from AttributeErrorRouter bubbles up"
     1703        router.routers = []
     1704        b = Book.objects.create(title="Pro Django",
     1705                                published=datetime.date(2008, 12, 16))
     1706        router.routers = [AttributeErrorRouter()]
     1707        self.assertRaises(AttributeError, Book.objects.get, pk=b.pk)
     1708
     1709    def test_attribute_error_save(self):
     1710        "Check that the AttributeError from AttributeErrorRouter bubbles up"
    17031711        dive = Book()
    17041712        dive.title="Dive into Python"
    17051713        dive.published = datetime.date(2009, 5, 4)
    17061714        self.assertRaises(AttributeError, dive.save)
    17071715
     1716    def test_attribute_error_delete(self):
     1717        "Check that the AttributeError from AttributeErrorRouter bubbles up"
     1718        router.routers = []
     1719        b = Book.objects.create(title="Pro Django",
     1720                                published=datetime.date(2008, 12, 16))
     1721        p = Person.objects.create(name="Marty Alchin")
     1722        b.authors = [p]
     1723        b.editor = p
     1724        router.routers = [AttributeErrorRouter()]
     1725        self.assertRaises(AttributeError, b.delete)
     1726
     1727    def test_attribute_error_m2m(self):
     1728        "Check that the AttributeError from AttributeErrorRouter bubbles up"
     1729        router.routers = []
     1730        b = Book.objects.create(title="Pro Django",
     1731                                published=datetime.date(2008, 12, 16))
     1732        p = Person.objects.create(name="Marty Alchin")
     1733        router.routers = [AttributeErrorRouter()]
     1734        self.assertRaises(AttributeError, setattr, b, 'authors', [p])
     1735
    17081736class ModelMetaRouter(object):
    17091737    "A router to ensure model arguments are real model classes"
    17101738    def db_for_write(self, model, **hints):
    17111739        if not hasattr(model, '_meta'):
    17121740            raise ValueError
    17131741
    1714 class RouterM2MThroughTestCase(TestCase):
     1742class RouterThroughTestCase(TestCase):
    17151743    multi_db = True
    17161744
    17171745    def setUp(self):
     
    17321760        b.authors.remove(p)
    17331761        # test clear
    17341762        b.authors.clear()
     1763        # test setattr
     1764        b.authors = [p]
     1765        # test _collect_sub_objects
     1766        b.delete()
     1767
     1768    def test_fk_through(self):
     1769        person = Person.objects.create(name='Bob')
     1770        pet = Pet.objects.create(owner=person, name='Wart')
     1771        # test _collect_sub_objects
     1772        person.delete()
Back to Top