#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:  Cleanup/optimization  |         Status:  new
              Component:  Documentation         |        Version:  2.1
               Severity:  Normal                |       Keywords:
           Triage Stage:  Unreviewed            |      Has patch:  0
    Needs documentation:  0                     |    Needs tests:  0
Patch needs improvement:  0                     |  Easy pickings:  0
                  UI/UX:  0                     |
------------------------------------------------+------------------------
 Trying to alter a many to many relation when saving a model's instance is
 a common use case.

 [https://stackoverflow.com/a/53608043/2996101 For example], when creating
 a new user or altering an already existing user, programmatically add or
 remove groups he should(n't) belong to.

 This can be achieved by catching a post save signal and creating a
 decorator that uses
 
[https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.on_commit
 on_commit]:



 {{{

 def on_transaction_commit(func):
     ''' Create the decorator '''
     def inner(*args, **kwargs):
         transaction.on_commit(lambda: func(*args, **kwargs))

     return inner


 @receiver(
     post_save,
     sender=settings.AUTH_USER_MODEL,
 )
 @on_transaction_commit
 def group_delegation(instance, raw, **kwargs):
     do stuff()
 }}}


 Is it relevant to doc it as an example in the
 
[https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_save
 post_save] signal chapter?

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30022>
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/051.bb1d8fbf7be8ac4c55ae644fed38ee66%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to