Code

Ticket #8669: create-always-inserts.v2.diff

File create-always-inserts.v2.diff, 1.9 KB (added by Richard Davies <richard.davies@…>, 6 years ago)
Line 
1Index: db/models/fields/related.py
2===================================================================
3--- db/models/fields/related.py (revision 8706)
4+++ db/models/fields/related.py (working copy)
5@@ -301,9 +301,10 @@
6             add.alters_data = True
7 
8             def create(self, **kwargs):
9-                new_obj = self.model(**kwargs)
10-                self.add(new_obj)
11-                return new_obj
12+                # Update kwargs with the related object that this
13+                # ForeignRelatedObjectsDescriptor knows about.
14+                kwargs.update({rel_field.name: instance})
15+                return super(RelatedManager, self).create(**kwargs)
16             create.alters_data = True
17 
18             def get_or_create(self, **kwargs):
19@@ -405,8 +406,7 @@
20             # from the method lookup table, as we do with add and remove.
21             if through is not None:
22                 raise AttributeError, "Cannot use create() on a ManyToManyField which specifies an intermediary model. Use %s's Manager instead." % through
23-            new_obj = self.model(**kwargs)
24-            new_obj.save()
25+            new_obj = super(ManyRelatedManager, self).create(**kwargs)
26             self.add(new_obj)
27             return new_obj
28         create.alters_data = True
29Index: contrib/contenttypes/generic.py
30===================================================================
31--- contrib/contenttypes/generic.py     (revision 8706)
32+++ contrib/contenttypes/generic.py     (working copy)
33@@ -272,9 +272,7 @@
34         def create(self, **kwargs):
35             kwargs[self.content_type_field_name] = self.content_type
36             kwargs[self.object_id_field_name] = self.pk_val
37-            obj = self.model(**kwargs)
38-            obj.save()
39-            return obj
40+            return super(superclass, self).create(**kwargs)
41         create.alters_data = True
42 
43     return GenericRelatedObjectManager