向 Django 信号添加参数
如何向 Django 信号添加参数以便关联的接收器可以使用这些参数?阅读文档我无法弄清楚自己。谢谢
示例:
def callback(sender, instance, **kwargs):
# I want to work with extra here!
extra = kwargs.get(extra, None)
if extra:
# do something with extra
pass
接收器以这种方式连接到信号:
pre_save.connect(callback)
how do I add arguments to Django signals so that the associated receiver can use those arguments? Reading the docs I've not been able to figure myself. Thanks
Example:
def callback(sender, instance, **kwargs):
# I want to work with extra here!
extra = kwargs.get(extra, None)
if extra:
# do something with extra
pass
The receiver is connected to the signal this way:
pre_save.connect(callback)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pre_save
不是您发送的信号,而是 Django 默认发出的:更改其参数可能非常复杂,并且可能会破坏某些东西。在您定义的信号上,您可以添加所需的所有参数;要了解如何操作,请查看文档(由 @Ignacio 友情链接)。
pre_save
is not a signal that you send, but rather Django emits it by default: changing its arguments is probably very complicated and likely to break things.On signals you define, you can add all the arguments you want; to see how, look at the docs (kindly linked by @Ignacio).