Ticket #13668: 13668.diff

File 13668.diff, 4.7 KB (added by David Gouldin, 14 years ago)
  • django/db/models/fields/related.py

     
    553553                        raise TypeError("'%s' instance expected" % self.model._meta.object_name)
    554554                    else:
    555555                        new_ids.add(obj)
    556                 db = router.db_for_write(self.through.__class__, instance=self.instance)
     556                db = router.db_for_write(self.through, instance=self.instance)
    557557                vals = self.through._default_manager.using(db).values_list(target_field_name, flat=True)
    558558                vals = vals.filter(**{
    559559                    source_field_name: self._pk_val,
     
    595595                    else:
    596596                        old_ids.add(obj)
    597597                # Work out what DB we're operating on
    598                 db = router.db_for_write(self.through.__class__, instance=self.instance)
     598                db = router.db_for_write(self.through, instance=self.instance)
    599599                # Send a signal to the other end if need be.
    600600                if self.reverse or source_field_name == self.source_field_name:
    601601                    # Don't send the signal when we are deleting the
     
    616616                        model=self.model, pk_set=old_ids, using=db)
    617617
    618618        def _clear_items(self, source_field_name):
    619             db = router.db_for_write(self.through.__class__, instance=self.instance)
     619            db = router.db_for_write(self.through, instance=self.instance)
    620620            # source_col_name: the PK colname in join_table for the source object
    621621            if self.reverse or source_field_name == self.source_field_name:
    622622                # Don't send the signal when we are clearing the
  • django/db/utils.py

     
    123123            chosen_db = None
    124124            for router in self.routers:
    125125                try:
    126                     chosen_db = getattr(router, action)(model, **hints)
    127                     if chosen_db:
    128                         return chosen_db
     126                    router_action = getattr(router, action)
    129127                except AttributeError:
    130128                    # If the router doesn't have a method, skip to the next one.
    131                     pass
     129                    continue
     130                chosen_db = router_action(model, **hints)
     131                if chosen_db:
     132                    return chosen_db
    132133            try:
    133134                return hints['instance']._state.db or DEFAULT_DB_ALIAS
    134135            except KeyError:
  • tests/regressiontests/multiple_database/tests.py

     
    17591759        b.authors.clear()
    17601760        self._write_to_default()
    17611761        self.assertEqual(receiver._database, "other")
     1762
     1763class AttributeErrorRouter(object):
     1764    "A router to test the exception handling of ConnectionRouter"
     1765    def db_for_read(self, model, **hints):
     1766        raise AttributeError
     1767
     1768    def db_for_write(self, model, **hints):
     1769        raise AttributeError
     1770
     1771class RouterAttributeErrorTestCase(TestCase):
     1772    multi_db = True
     1773
     1774    def setUp(self):
     1775        self.old_routers = router.routers
     1776        router.routers = [AttributeErrorRouter()]
     1777
     1778    def tearDown(self):
     1779        router.routers = self.old_routers
     1780
     1781    def test_attribute_error(self):
     1782        "Check that the AttributeError from AttributeErrorRouter bubbles up"
     1783        dive = Book()
     1784        dive.title="Dive into Python"
     1785        dive.published = datetime.date(2009, 5, 4)
     1786        self.assertRaises(AttributeError, dive.save)
     1787
     1788class ModelMetaRouter(object):
     1789    "A router to ensure model arguments are real model classes"
     1790    def db_for_write(self, model, **hints):
     1791        if not hasattr(model, '_meta'):
     1792            raise ValueError
     1793
     1794class RouterM2MThroughTestCase(TestCase):
     1795    multi_db = True
     1796
     1797    def setUp(self):
     1798        self.old_routers = router.routers
     1799        router.routers = [ModelMetaRouter()]
     1800
     1801    def tearDown(self):
     1802        router.routers = self.old_routers
     1803
     1804    def test_m2m_through(self):
     1805        b = Book.objects.create(title="Pro Django",
     1806                                published=datetime.date(2008, 12, 16))
     1807
     1808        p = Person.objects.create(name="Marty Alchin")
     1809        # test add
     1810        b.authors.add(p)
     1811        # test remove
     1812        b.authors.remove(p)
     1813        # test clear
     1814        b.authors.clear()
Back to Top