Ticket #13668: 13668.diff
File 13668.diff, 4.7 KB (added by , 14 years ago) |
---|
-
django/db/models/fields/related.py
553 553 raise TypeError("'%s' instance expected" % self.model._meta.object_name) 554 554 else: 555 555 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) 557 557 vals = self.through._default_manager.using(db).values_list(target_field_name, flat=True) 558 558 vals = vals.filter(**{ 559 559 source_field_name: self._pk_val, … … 595 595 else: 596 596 old_ids.add(obj) 597 597 # 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) 599 599 # Send a signal to the other end if need be. 600 600 if self.reverse or source_field_name == self.source_field_name: 601 601 # Don't send the signal when we are deleting the … … 616 616 model=self.model, pk_set=old_ids, using=db) 617 617 618 618 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) 620 620 # source_col_name: the PK colname in join_table for the source object 621 621 if self.reverse or source_field_name == self.source_field_name: 622 622 # Don't send the signal when we are clearing the -
django/db/utils.py
123 123 chosen_db = None 124 124 for router in self.routers: 125 125 try: 126 chosen_db = getattr(router, action)(model, **hints) 127 if chosen_db: 128 return chosen_db 126 router_action = getattr(router, action) 129 127 except AttributeError: 130 128 # 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 132 133 try: 133 134 return hints['instance']._state.db or DEFAULT_DB_ALIAS 134 135 except KeyError: -
tests/regressiontests/multiple_database/tests.py
1759 1759 b.authors.clear() 1760 1760 self._write_to_default() 1761 1761 self.assertEqual(receiver._database, "other") 1762 1763 class 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 1771 class 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 1788 class 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 1794 class 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()