Ticket #14948: django-1.2-router_related_accessor.patch

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

patch with testcase for django-1.2, trunk will need to be patched as well

  • 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
  • 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,
     
    601601                        instance=self.instance, reverse=self.reverse,
    602602                        model=self.model, pk_set=old_ids)
    603603                # 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)
    605605                self.through._default_manager.using(db).filter(**{
    606606                    source_field_name: self._pk_val,
    607607                    '%s__in' % target_field_name: old_ids
     
    621621                signals.m2m_changed.send(sender=rel.through, action="pre_clear",
    622622                    instance=self.instance, reverse=self.reverse,
    623623                    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)
    625625            self.through._default_manager.using(db).filter(**{
    626626                source_field_name: self._pk_val
    627627            }).delete()
  • tests/regressiontests/multiple_database/tests.py

     
    44from StringIO import StringIO
    55
    66from django.conf import settings
    7 from django.contrib.auth.models import User
     7from django.contrib.auth.models import User, Group
    88from django.core import management
    99from django.db import connections, router, DEFAULT_DB_ALIAS
    1010from django.db.utils import ConnectionRouter
     
    973973    def db_for_write(self, model, **hints):
    974974        return 'writer'
    975975
     976class FaultyRouter(object):
     977    # a miss configured router
     978    def db_for_write(self, model, **hints):
     979        raise AttributeError
     980
    976981class RouterTestCase(TestCase):
    977982    multi_db = True
    978983
     
    15751580        command_output = new_io.getvalue().strip()
    15761581        self.assertTrue('"email": "alice@example.com",' in command_output)
    15771582
     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
    15781598_missing = object()
    15791599class UserProfileTestCase(TestCase):
    15801600    def setUp(self):
Back to Top