How to pass kwargs from save to post_save signal

前端 未结 3 1908
南方客
南方客 2020-12-23 21:16

I\'m wiring up a custom post_save signal and noticed that I can\'t seem to find an easy way to pass a set of kwargs.

During the save itself (inside a custom form)

3条回答
  •  滥情空心
    2020-12-23 21:48

    We can pass additional arguments as below:

    def save(self, commit=True):
        user = super(CustomFormThing, self).save(commit=False)
        #set some other attrs on user here ...
        if commit:
            user.save(signal_kwargs={'_some': 'some', '_other': 'other'})
    
        return user
    

    And you can get it from the post save method like:

    @receiver(post_save, sender=User)
    def create_profile(sender, instance, created, **kwargs):
        some_id = kwargs.get('some', None)
        other_id = kwargs.get('other', None)
        if created:
            #do something with the kwargs above...
    

提交回复
热议问题