﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30022	Doc how to combine post_save signal with on_commit to alter a m2m relation when saving a model instance	George Tantiras	nobody	"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?"	Cleanup/optimization	closed	Documentation	2.1	Normal	wontfix			Unreviewed	0	0	0	0	0	0
