Code

Ticket #13637: django-ct-dbs.diff

File django-ct-dbs.diff, 4.8 KB (added by Alex, 4 years ago)
Line 
1diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py
2index 40c3336..4bafe81 100644
3--- a/django/contrib/contenttypes/generic.py
4+++ b/django/contrib/contenttypes/generic.py
5@@ -3,7 +3,6 @@ Classes allowing "generic" relations through ContentType and object-id fields.
6 """
7 
8 from django.core.exceptions import ObjectDoesNotExist
9-from django.db import connection
10 from django.db.models import signals
11 from django.db import models, router
12 from django.db.models.fields.related import RelatedField, Field, ManyToManyRel
13@@ -183,25 +182,20 @@ class ReverseGenericRelatedObjectsDescriptor(object):
14         if instance is None:
15             return self
16 
17-        # This import is done here to avoid circular import importing this module
18-        from django.contrib.contenttypes.models import ContentType
19-
20         # Dynamically create a class that subclasses the related model's
21         # default manager.
22         rel_model = self.field.rel.to
23         superclass = rel_model._default_manager.__class__
24         RelatedManager = create_generic_related_manager(superclass)
25-
26-        qn = connection.ops.quote_name
27+       
28+        # Imported here to avoid circular import
29+        from django.contrib.contenttypes.models import ContentType
30 
31         manager = RelatedManager(
32             model = rel_model,
33             instance = instance,
34             symmetrical = (self.field.rel.symmetrical and instance.__class__ == rel_model),
35-            join_table = qn(self.field.m2m_db_table()),
36-            source_col_name = qn(self.field.m2m_column_name()),
37-            target_col_name = qn(self.field.m2m_reverse_name()),
38-            content_type = ContentType.objects.db_manager(instance._state.db).get_for_model(instance),
39+            content_type = ContentType.objects.get_for_model(instance),
40             content_type_field_name = self.field.content_type_field_name,
41             object_id_field_name = self.field.object_id_field_name
42         )
43@@ -224,28 +218,23 @@ def create_generic_related_manager(superclass):
44     """
45 
46     class GenericRelatedObjectManager(superclass):
47-        def __init__(self, model=None, core_filters=None, instance=None, symmetrical=None,
48-                     join_table=None, source_col_name=None, target_col_name=None, content_type=None,
49-                     content_type_field_name=None, object_id_field_name=None):
50-
51+        def __init__(self, model=None, core_filters=None, instance=None,
52+            symmetrical=None, content_type=None, content_type_field_name=None,
53+            object_id_field_name=None):
54             super(GenericRelatedObjectManager, self).__init__()
55             self.core_filters = core_filters or {}
56             self.model = model
57-            self.content_type = content_type
58             self.symmetrical = symmetrical
59             self.instance = instance
60-            self.join_table = join_table
61-            self.join_table = model._meta.db_table
62-            self.source_col_name = source_col_name
63-            self.target_col_name = target_col_name
64+            self.content_type = content_type
65             self.content_type_field_name = content_type_field_name
66             self.object_id_field_name = object_id_field_name
67             self.pk_val = self.instance._get_pk_val()
68-
69+       
70         def get_query_set(self):
71             db = self._db or router.db_for_read(self.model, instance=self.instance)
72             query = {
73-                '%s__pk' % self.content_type_field_name : self.content_type.id,
74+                '%s__pk' % self.content_type_field_name : self.content_type.pk,
75                 '%s__exact' % self.object_id_field_name : self.pk_val,
76             }
77             return superclass.get_query_set(self).using(db).filter(**query)
78@@ -253,7 +242,8 @@ def create_generic_related_manager(superclass):
79         def add(self, *objs):
80             for obj in objs:
81                 if not isinstance(obj, self.model):
82-                    raise TypeError("'%s' instance expected" % self.model._meta.object_name)
83+                    raise TypeError("'%s' instance expected" %
84+                        self.model._meta.object_name)
85                 setattr(obj, self.content_type_field_name, self.content_type)
86                 setattr(obj, self.object_id_field_name, self.pk_val)
87                 obj.save()
88@@ -272,9 +262,9 @@ def create_generic_related_manager(superclass):
89         clear.alters_data = True
90 
91         def create(self, **kwargs):
92+            db = router.db_for_write(self.model, instance=self.instance)
93             kwargs[self.content_type_field_name] = self.content_type
94             kwargs[self.object_id_field_name] = self.pk_val
95-            db = router.db_for_write(self.model, instance=self.instance)
96             return super(GenericRelatedObjectManager, self).using(db).create(**kwargs)
97         create.alters_data = True
98