如何将kwargs从保存到post_save信号 [英] How to pass kwargs from save to post_save signal
本文介绍了如何将kwargs从保存到post_save信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在配置一个自定义post_save信号,并注意到我似乎找不到一个简单的方法来传递一组kwargs。
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)
def save(self, commit=True):
user = super(CustomFormThing, self).save(commit=False)
#set some other attrs on user here ...
if commit:
user.save()
return user
然后在我的自定义post_save钩子我有以下(但从来没有得到任何kwargs)
Then inside my custom post_save hook I have the following (but never get any kwargs)
@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...
如何将kwargs从保存传递到post_save事件? >
How might I pass kwargs from the save to the post_save event?
推荐答案
内置n信号由Django发送,所以你不能控制他们的kwargs。
Built-in signals are sent by Django, so you can't control their kwargs.
你可以:
- 定义并发送您自己的信号。
-
在模型实例中存储附加信息。像这样
- Define and send your own signals.
Store additional info in model instance. Like this
def save(self, commit=True):
user = super(CustomFormThing, self).save(commit=False)
#set some other attrs on user here ...
user._some = 'some'
user._other = 'other'
if commit:
user.save()
return user
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
some_id = getattr(instance, '_some', None)
other_id = getattr(instance, '_other', None)
if created:
#do something with the kwargs above...
这篇关于如何将kwargs从保存到post_save信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文