I am wondering if it is possible to selectively suppress a Django signal (for example, post_saveor post_init) when creating an object or, alternatively, send it certain parameters.
I have an object Userthat can be created in many ways and places in my code. Therefore, to automatically assign a custom object Profileto each User, I use a signal post_save. However, in one specific case, there is additional information that I want to associate with the created object Profile. Passing it as arguments for the signal post_savewould be great, but this is not possible.
Another option is to manually create the object Profile, but then I need to, after saving User, otherwise it Profilecannot be bound to the instance User. However, saving an instance Userleads to the creation of another Profileusing the function called by the signal.
And I canβt just get the newly created object Profile, as this leads to an error 'Profile' object is unsubscriptable. Any tips?
Update:
Here is an example of a possible situation:
def createUserProfile(sender, instance, created, **kwargs):
if created:
profile, created = Profile.objects.get_or_create(user=instance)
if extra_param:
profile.extra_param = extra_param
profile.save()
post_save.connect(createUserProfile, sender=User)
def signup(request):
...
extra_param = 'param'
user.save()
How do I get a variable extra_paramin a method signupfor the createUserProfile method, where it should be saved as part of the object Profile?
source
share