#30022: Doc how to combine post_save signal with on_commit to alter a m2m 
relation
when saving a model instance
-------------------------------------+-------------------------------------
     Reporter:  George Tantiras      |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Documentation        |                  Version:  2.1
     Severity:  Normal               |               Resolution:  wontfix
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

 * status:  new => closed
 * resolution:   => wontfix


Comment:

 Thank you for the suggestion but it feels like a pretty specific example
 to document as this is not something you want to do in all cases.

 If I understand where you are coming from you want to make sure changes
 performed in `save()` are committed to the database in the same
 transaction as the m2m alterations.

 In your "group" case your suggested approach will work fine in cases where
 `save()` in not called in within a `transaction.atomic()` block will have
 surprising results if this doesn't hold true.

 For example in

 {{{#!python
 @transaction.atomic
 def accept_group_invite(request, group_id):
     validate_and_add_to_group(request.user, group_id)
     # The below line would always fail in your case because the on_commit
     # receiver wouldn't be called until exiting this function.
     if request.user.has_perm('group_permission'):
         do_something()
     ...
 }}}

 `do_something()` would never be called because
 `validate_and_add_to_group()`'s `save()` calls that would trigger the
 `post_save` receiver would only queue the group additions to be performed
 `on_commit` which wouldn't happen until the `accept_group_invite` function
 exits.

 If you want to make sure both `save()` and its `pre_save` and `post_save`
 side effects are performed in as an atomic operation (either succeed or
 fail) then you should simply wrap your `save()` calls in an `atomic`
 block.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30022#comment:1>
Django <https://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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/066.849b85001adf0983d0dadfef099d4042%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to