Ticket #14948: django-1.2-router_related_accessor.patch
File django-1.2-router_related_accessor.patch, 4.8 KB (added by , 14 years ago) |
---|
-
django/db/models/base.py
612 612 613 613 for related in self._meta.get_all_related_many_to_many_objects(): 614 614 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) 616 616 opts = related.field.rel.through._meta 617 617 reverse_field_name = related.field.m2m_reverse_field_name() 618 618 nullable = opts.get_field(reverse_field_name).null … … 622 622 623 623 for f in self._meta.many_to_many: 624 624 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) 626 626 opts = f.rel.through._meta 627 627 field_name = f.m2m_field_name() 628 628 nullable = opts.get_field(field_name).null -
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, … … 601 601 instance=self.instance, reverse=self.reverse, 602 602 model=self.model, pk_set=old_ids) 603 603 # Remove the specified objects from the join table 604 db = router.db_for_write(self.through .__class__, instance=self.instance)604 db = router.db_for_write(self.through, instance=self.instance) 605 605 self.through._default_manager.using(db).filter(**{ 606 606 source_field_name: self._pk_val, 607 607 '%s__in' % target_field_name: old_ids … … 621 621 signals.m2m_changed.send(sender=rel.through, action="pre_clear", 622 622 instance=self.instance, reverse=self.reverse, 623 623 model=self.model, pk_set=None) 624 db = router.db_for_write(self.through .__class__, instance=self.instance)624 db = router.db_for_write(self.through, instance=self.instance) 625 625 self.through._default_manager.using(db).filter(**{ 626 626 source_field_name: self._pk_val 627 627 }).delete() -
tests/regressiontests/multiple_database/tests.py
4 4 from StringIO import StringIO 5 5 6 6 from django.conf import settings 7 from django.contrib.auth.models import User 7 from django.contrib.auth.models import User, Group 8 8 from django.core import management 9 9 from django.db import connections, router, DEFAULT_DB_ALIAS 10 10 from django.db.utils import ConnectionRouter … … 973 973 def db_for_write(self, model, **hints): 974 974 return 'writer' 975 975 976 class FaultyRouter(object): 977 # a miss configured router 978 def db_for_write(self, model, **hints): 979 raise AttributeError 980 976 981 class RouterTestCase(TestCase): 977 982 multi_db = True 978 983 … … 1575 1580 command_output = new_io.getvalue().strip() 1576 1581 self.assertTrue('"email": "alice@example.com",' in command_output) 1577 1582 1583 def test_related_accessor(self): 1584 "Check that related accessors validate the through model" 1585 user = User.objects.create_user('alice', 'alice@example.com') 1586 group = Group.objects.create(name='Users') 1587 user.groups = [group] 1588 self.assertEquals(Group.objects.using('other').filter(user=user).count(), 1) 1589 1590 def test_faulty_router(self): 1591 "Check that a faulty routers exceptions are raised" 1592 user = User.objects.create_user('alice', 'alice@example.com') 1593 group = Group.objects.create(name='Users') 1594 router.routers = [FaultyRouter(), AuthRouter()] 1595 self.assertRaises(AttributeError, User.objects.create_user, 'bob', 'bob@example.com') 1596 self.assertRaises(AttributeError, setattr, user, 'groups', [group]) 1597 1578 1598 _missing = object() 1579 1599 class UserProfileTestCase(TestCase): 1580 1600 def setUp(self):