Code

Ticket #10672: 10672.2.diff

File 10672.2.diff, 3.8 KB (added by UloPe, 5 years ago)

Updated to trunk (r10836)

Line 
1Index: django/db/models/base.py
2===================================================================
3--- django/db/models/base.py    (revision 10836)
4+++ django/db/models/base.py    (working copy)
5@@ -420,13 +420,14 @@
6         ('raw' and 'cls').
7         """
8         assert not (force_insert and force_update)
9-        if not cls:
10-            cls = self.__class__
11+        klass = cls
12+        if not klass:
13+            klass = self.__class__
14             meta = self._meta
15             signal = True
16             signals.pre_save.send(sender=self.__class__, instance=self, raw=raw)
17         else:
18-            meta = cls._meta
19+            meta = klass._meta
20             signal = False
21 
22         # If we are in a raw save, save the object exactly as presented.
23@@ -441,11 +442,9 @@
24                 if field and getattr(self, parent._meta.pk.attname) is None and getattr(self, field.attname) is not None:
25                     setattr(self, parent._meta.pk.attname, getattr(self, field.attname))
26 
27-                self.save_base(cls=parent)
28+                record_exists = not self.save_base(cls=parent)
29                 if field:
30                     setattr(self, field.attname, self._get_pk_val(parent._meta))
31-            if meta.proxy:
32-                return
33 
34         if not meta.proxy:
35             non_pks = [f for f in meta.local_fields if not f.primary_key]
36@@ -454,7 +453,7 @@
37             pk_val = self._get_pk_val(meta)
38             pk_set = pk_val is not None
39             record_exists = True
40-            manager = cls._base_manager
41+            manager = klass._base_manager
42             if pk_set:
43                 # Determine whether a record with the primary key already exists.
44                 if (force_update or (not force_insert and
45@@ -496,6 +495,9 @@
46             signals.post_save.send(sender=self.__class__, instance=self,
47                 created=(not record_exists), raw=raw)
48 
49+        if cls:
50+            return (not record_exists)
51+
52     save_base.alters_data = True
53 
54     def _collect_sub_objects(self, seen_objs, parent=None, nullable=False):
55Index: tests/modeltests/proxy_models/models.py
56===================================================================
57--- tests/modeltests/proxy_models/models.py     (revision 10836)
58+++ tests/modeltests/proxy_models/models.py     (working copy)
59@@ -259,6 +259,30 @@
60 >>> OtherPerson._default_manager.all()
61 [<OtherPerson: barney>, <OtherPerson: wilma>]
62 
63+# Test save signals for proxy models
64+>>> from django.db.models import signals
65+>>> def proxy_pre_save(*args, **kwargs): print u"proxy pre save"
66+...
67+>>> def proxy_post_save(*args, **kwargs): print u"proxy post save"
68+...
69+>>> signals.pre_save.connect(proxy_pre_save, sender=MyPerson)
70+>>> signals.post_save.connect(proxy_post_save, sender=MyPerson)
71+>>> dino = MyPerson.objects.create(name=u"dino")
72+proxy pre save
73+proxy post save
74+
75+# And test save signals for proxy proxy models
76+>>> from django.db.models import signals
77+>>> def proxy_proxy_pre_save(*args, **kwargs): print u"proxy proxy pre save"
78+...
79+>>> def proxy_proxy_post_save(*args, **kwargs): print u"proxy proxy post save"
80+...
81+>>> signals.pre_save.connect(proxy_proxy_pre_save, sender=MyPersonProxy)
82+>>> signals.post_save.connect(proxy_proxy_post_save, sender=MyPersonProxy)
83+>>> dino = MyPersonProxy.objects.create(name=u"pebbles")
84+proxy proxy pre save
85+proxy proxy post save
86+
87 # A proxy has the same content type as the model it is proxying for (at the
88 # storage level, it is meant to be essentially indistinguishable).
89 >>> ctype = ContentType.objects.get_for_model
90@@ -266,7 +290,7 @@
91 True
92 
93 >>> MyPersonProxy.objects.all()
94-[<MyPersonProxy: barney>, <MyPersonProxy: fred>]
95+[<MyPersonProxy: barney>, <MyPersonProxy: dino>, <MyPersonProxy: fred>, <MyPersonProxy: pebbles>]
96 
97 >>> u = User.objects.create(name='Bruce')
98 >>> User.objects.all()