How to pass kwargs from save to post_save signal

前端 未结 3 1907
南方客
南方客 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:37

    I don't think there's a way to pass a separate set of kwargs. What args do you want in addition to the User attributes? You can access the User attributes in the signal handler on the instance argument. instance will be the User object that just got saved.

    If there are other things you want to pass along, I suppose you could try to use the instance arg as a carrier pigeon:

    def save(self, commit=True):
        user = super(CustomFormThing, self).save(commit=False)
        user.foo = 'bar'
        if commit:
            user.save()
        ...
    

    Then in the handler:

    def create_profile(sender, instance, created, **kwargs):
        myfoo = instance.foo
    

    But the above is untested, and I'm not sure it will even work.

提交回复
热议问题