[Django] #12149: pre_save is not called before the overridden save() method on a model

View: New views
3 Messages — Rating Filter:   Alert me  

[Django] #12149: pre_save is not called before the overridden save() method on a model

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#12149: pre_save is not called before the overridden save() method on a model
---------------------------+------------------------------------------------
 Reporter:  siddhi         |       Owner:  nobody    
   Status:  new            |   Milestone:            
Component:  Uncategorized  |     Version:  1.1      
 Keywords:                 |       Stage:  Unreviewed
Has_patch:  0              |  
---------------------------+------------------------------------------------
 If I have a model where I override the save() method, then the pre_save
 signal is not sent before the save method is called.

 Example:

 {{{
 class MyModel(models.Model):
     name = models.CharField(max_length=20)

     def save(self, force_insert=False, force_update=False):
         if self.name == "dont_save":
             return
         super(Project, self).save(force_insert, force_delete)
 }}}

 {{{
 def presave_handler(sender, instance, **kwargs):
     instance.name = "dont_save"

 signals.pre_save.connect(presave_handler, sender=MyModel,
 dispatch_uid="abc")
 }}}

 In the above case, the flow goes like this

 1. call overridden save method
 1. check the condition in save method (condition is false)
 1. call super
 1. call pre_save
 1. set name to "dont_save"
 1. object saved to database with name = "dont_save"

 This is rather unintuitive that the pre_save gets called in the middle of
 the save method. Also, any processing done in the pre_save cannot be
 handled in the save method as the flow has gone to the super class by
 then.

 The expected flow should be like this

 1. call overridden save method
 1. call pre_save
 1. set name to "dont_save"
 1. execution enters save method
 1. check condition in overridden save method (condition is true)
 1. return without saving

--
Ticket URL: <http://code.djangoproject.com/ticket/12149>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #12149: pre_save is not called before the overridden save() method on a model

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#12149: pre_save is not called before the overridden save() method on a model
------------------------------------+---------------------------------------
          Reporter:  siddhi         |         Owner:  nobody
            Status:  closed         |     Milestone:        
         Component:  Uncategorized  |       Version:  1.1  
        Resolution:  invalid        |      Keywords:        
             Stage:  Unreviewed     |     Has_patch:  0    
        Needs_docs:  0              |   Needs_tests:  0    
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Changes (by kmtracey):

  * status:  new => closed
  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0
  * resolution:  => invalid

Old description:

> If I have a model where I override the save() method, then the pre_save
> signal is not sent before the save method is called.
>
> Example:
>
> {{{
> class MyModel(models.Model):
>     name = models.CharField(max_length=20)
>
>     def save(self, force_insert=False, force_update=False):
>         if self.name == "dont_save":
>             return
>         super(Project, self).save(force_insert, force_delete)
> }}}
>
> {{{
> def presave_handler(sender, instance, **kwargs):
>     instance.name = "dont_save"
>
> signals.pre_save.connect(presave_handler, sender=MyModel,
> dispatch_uid="abc")
> }}}
>
> In the above case, the flow goes like this
>
> 1. call overridden save method
> 1. check the condition in save method (condition is false)
> 1. call super
> 1. call pre_save
> 1. set name to "dont_save"
> 1. object saved to database with name = "dont_save"
>
> This is rather unintuitive that the pre_save gets called in the middle of
> the save method. Also, any processing done in the pre_save cannot be
> handled in the save method as the flow has gone to the super class by
> then.
>
> The expected flow should be like this
>
> 1. call overridden save method
> 1. call pre_save
> 1. set name to "dont_save"
> 1. execution enters save method
> 1. check condition in overridden save method (condition is true)
> 1. return without saving

New description:

 If I have a model where I override the save() method, then the pre_save
 signal is not sent before the save method is called.

 Example:

 {{{
 class MyModel(models.Model):
     name = models.CharField(max_length=20)

     def save(self, force_insert=False, force_update=False):
         if self.name == "dont_save":
             return
         super(Project, self).save(force_insert, force_delete)
 }}}

 {{{
 def presave_handler(sender, instance, **kwargs):
     instance.name = "dont_save"

 signals.pre_save.connect(presave_handler, sender=MyModel,
 dispatch_uid="abc")
 }}}

 In the above case, the flow goes like this

  1. call overridden save method
  1. check the condition in save method (condition is false)
  1. call super
  1. call pre_save
  1. set name to "dont_save"
  1. object saved to database with name = "dont_save"

 This is rather unintuitive that the pre_save gets called in the middle of
 the save method. Also, any processing done in the pre_save cannot be
 handled in the save method as the flow has gone to the super class by
 then.

 The expected flow should be like this

  1. call overridden save method
  1. call pre_save
  1. set name to "dont_save"
  1. execution enters save method
  1. check condition in overridden save method (condition is true)
  1. return without saving

Comment:

 (Fixed formatting.  Note you've got to put a space before the `1.` in your
 lists to get them to format properly.)

 The signals doc: http://docs.djangoproject.com/en/dev/ref/signals/#module-
 django.db.models.signals

 notes that if you override `save()` you must call the parent class method
 in order for the signals to be sent.  That makes it pretty clear the
 parent class code is what is going to send the signals.  It isn't clear to
 me how you expect a signal to get sent at step 2 here:

  1. call overridden save method
  1. call pre_save
  1. set name to "dont_save".

 At that point execution is in your own code, how is Django supposed to
 cause a signal to be sent?

--
Ticket URL: <http://code.djangoproject.com/ticket/12149#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: [Django] #12149: pre_save is not called before the overridden save() method on a model

by noreply-71 :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

#12149: pre_save is not called before the overridden save() method on a model
------------------------------------+---------------------------------------
          Reporter:  siddhi         |         Owner:  nobody
            Status:  reopened       |     Milestone:        
         Component:  Uncategorized  |       Version:  1.1  
        Resolution:                 |      Keywords:        
             Stage:  Unreviewed     |     Has_patch:  0    
        Needs_docs:  0              |   Needs_tests:  0    
Needs_better_patch:  0              |  
------------------------------------+---------------------------------------
Changes (by siddhi):

  * status:  closed => reopened
  * resolution:  invalid =>

Comment:

 Replying to [comment:1 kmtracey]:

 > It isn't clear to me how you expect a signal to get sent at step 2 here:

  1. call overridden save method
  1. call pre_save
  1. set name to "dont_save".

 > At that point execution is in your own code, how is Django supposed to
 cause a signal to be sent?

 As currently implemented, the signal can't be sent. But it should. That is
 the bug :) Perhaps steps 1 and 2 should be exchanged in that flow.

 The design for sending the signals needs to be changed. Maybe the
 metaclass should automatically decorate the save method to send the signal
 before and after, instead of sending it within the parent save(). The
 right design and alternatives can be discussed.

 As it stands, you cant use the changes from the pre_save within the custom
 save method. By the time pre_save is called execution is already out of
 the custom save and in the superclass. This is very illogical. You would
 expect a signal called pre_save would be triggered before the save.

--
Ticket URL: <http://code.djangoproject.com/ticket/12149#comment:2>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django updates" group.
To post to this group, send email to django-updates@...
To unsubscribe from this group, send email to django-updates+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---