Ticket #13637: django-ct-dbs.diff

File django-ct-dbs.diff, 4.8 KB (added by Alex Gaynor, 14 years ago)
  • django/contrib/contenttypes/generic.py

    diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
    index 40c3336..4bafe81 100644
    a b Classes allowing "generic" relations through ContentType and object-id fields.  
    33"""
    44
    55from django.core.exceptions import ObjectDoesNotExist
    6 from django.db import connection
    76from django.db.models import signals
    87from django.db import models, router
    98from django.db.models.fields.related import RelatedField, Field, ManyToManyRel
    class ReverseGenericRelatedObjectsDescriptor(object):  
    183182        if instance is None:
    184183            return self
    185184
    186         # This import is done here to avoid circular import importing this module
    187         from django.contrib.contenttypes.models import ContentType
    188 
    189185        # Dynamically create a class that subclasses the related model's
    190186        # default manager.
    191187        rel_model = self.field.rel.to
    192188        superclass = rel_model._default_manager.__class__
    193189        RelatedManager = create_generic_related_manager(superclass)
    194 
    195         qn = connection.ops.quote_name
     190       
     191        # Imported here to avoid circular import
     192        from django.contrib.contenttypes.models import ContentType
    196193
    197194        manager = RelatedManager(
    198195            model = rel_model,
    199196            instance = instance,
    200197            symmetrical = (self.field.rel.symmetrical and instance.__class__ == rel_model),
    201             join_table = qn(self.field.m2m_db_table()),
    202             source_col_name = qn(self.field.m2m_column_name()),
    203             target_col_name = qn(self.field.m2m_reverse_name()),
    204             content_type = ContentType.objects.db_manager(instance._state.db).get_for_model(instance),
     198            content_type = ContentType.objects.get_for_model(instance),
    205199            content_type_field_name = self.field.content_type_field_name,
    206200            object_id_field_name = self.field.object_id_field_name
    207201        )
    def create_generic_related_manager(superclass):  
    224218    """
    225219
    226220    class GenericRelatedObjectManager(superclass):
    227         def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None,
    228                      join_table=None, source_col_name=None, target_col_name=None, content_type=None,
    229                      content_type_field_name=None, object_id_field_name=None):
    230 
     221        def __init__(self, model=None, core_filters=None, instance=None,
     222            symmetrical=None, content_type=None, content_type_field_name=None,
     223            object_id_field_name=None):
    231224            super(GenericRelatedObjectManager, self).__init__()
    232225            self.core_filters = core_filters or {}
    233226            self.model = model
    234             self.content_type = content_type
    235227            self.symmetrical = symmetrical
    236228            self.instance = instance
    237             self.join_table = join_table
    238             self.join_table = model._meta.db_table
    239             self.source_col_name = source_col_name
    240             self.target_col_name = target_col_name
     229            self.content_type = content_type
    241230            self.content_type_field_name = content_type_field_name
    242231            self.object_id_field_name = object_id_field_name
    243232            self.pk_val = self.instance._get_pk_val()
    244 
     233       
    245234        def get_query_set(self):
    246235            db = self._db or router.db_for_read(self.model, instance=self.instance)
    247236            query = {
    248                 '%s__pk' % self.content_type_field_name : self.content_type.id,
     237                '%s__pk' % self.content_type_field_name : self.content_type.pk,
    249238                '%s__exact' % self.object_id_field_name : self.pk_val,
    250239            }
    251240            return superclass.get_query_set(self).using(db).filter(**query)
    def create_generic_related_manager(superclass):  
    253242        def add(self, *objs):
    254243            for obj in objs:
    255244                if not isinstance(obj, self.model):
    256                     raise TypeError("'%s' instance expected" % self.model._meta.object_name)
     245                    raise TypeError("'%s' instance expected" %
     246                        self.model._meta.object_name)
    257247                setattr(obj, self.content_type_field_name, self.content_type)
    258248                setattr(obj, self.object_id_field_name, self.pk_val)
    259249                obj.save()
    def create_generic_related_manager(superclass):  
    272262        clear.alters_data = True
    273263
    274264        def create(self, **kwargs):
     265            db = router.db_for_write(self.model, instance=self.instance)
    275266            kwargs[self.content_type_field_name] = self.content_type
    276267            kwargs[self.object_id_field_name] = self.pk_val
    277             db = router.db_for_write(self.model, instance=self.instance)
    278268            return super(GenericRelatedObjectManager, self).using(db).create(**kwargs)
    279269        create.alters_data = True
    280270
Back to Top