How to stop recursion on Django's post_save signal
来源:互联网 发布:游族网络新游戏 编辑:程序博客网 时间:2024/06/06 19:01
In Django there's a signal called post_save which gets called after a model's save() method has been called. But what happens when you needed to update the instance inside the post_save like this:
1
def
do_stuff(sender,
*
*
kwargs):
2
kwargs[
'instance'
].save()
3
post_save.connect(do_stuff, sender
=
User)
This will fire another post_save signal that makes a recursion and local server stops. This can be solved by overriding the save() method instead and do processing there but I haven't seen a code where they override the user, instead they create profiles which isn't really what I need.
To solve the problem, you have to disconnect the signal before calling the save() method again while inside the post_save function and connect it again once its done.
1
def
do_stuff(sender,
*
*
kwargs):
2
post_save.disconnect(do_stuff, sender
=
User)
3
kwargs[
'instance'
].save()
4
post_save.connect(do_stuff, sender
=
User)
5
post_save.connect(do_stuff, sender
=
User)
This way when the save() method is called, it never fires another post_save signal because we've disconnected it.
- How to stop recursion on Django's post_save signal
- How to stop 'uninterruptible' process on Linux?
- django post_save post_delete
- How to start/restart/stop apache server on ubuntu
- How to start stop restart mysql on Mac
- How to Stop a Thread
- How to run RedBoot on SkyEye's SMDK2410 simulation
- How to run RedBoot on SkyEye's SMDK2410 simulation(2)
- Generic Netlink HOW-TO based on Jamal's original doc
- How to check the entitlements on my Application's Signature
- How to uninstall django
- How to start and stop exe
- How to stop linux sendmail service
- How to stop #including redundant headers?
- ADF HOW TO: How to stop adf:Poll
- How to input "EOF" signal in unix?
- How To Add a New Disk(s) to An Existing Diskgroup on RAC (Best Practices).
- How to Build Django document
- 搭建Web服务器Apache
- About ntoskrnl 版本
- 【读书笔记】usOS-II学习笔记(2008-11-4)
- .NET架构改造
- Linux 性能监测:Memory
- How to stop recursion on Django's post_save signal
- nyoj-73-比大小
- 6410之异常中断处理
- Conversion to Dalvik format failed with error 1错误解决
- dsPIC33F串口通讯中断接收发送数据-UART
- PHPCMS V9内容页点击量如何调用
- 对linux系统运行性能的分析
- svn: None of the environment variables SVN_EDITOR...问题解决
- Android 流媒体系列(二)