【问题标题】:Post_save received twice for one save even when using dispatch_uid即使使用 dispatch_uid,Post_save 也会收到两次保存
【发布时间】:2012-01-10 14:07:03
【问题描述】:

我的模型在单独的文件中:

models
\ 
 |__init__.py
 |event.py
 |a_thing.py
 |...

__init__.py 中导入每个模型,然后设置信号处理。

对于Event 模型,我需要一些post_save 处理。

这是__init__.py的截断版本:

from django.db.models.signals import post_save
from django.dispatch import receiver

from core.models.event import Event

# Event
@receiver(post_save, sender = Event)
def event_post_save(sender, dispatch_uid = 'nope', **kwargs):
    print kwargs.get('created')
    print '------'

每当我通过控制台保存Event 时,post_save 中的消息都会打印一次,但每当我使用管理界面时,它就会打印两次。这可能是因为我也在admin.py 中导入了模型。

是否有解决方法,以便我可以从管理界面保存 Event 对象,而不会触发两次 post_save

【问题讨论】:

    标签: django django-admin django-signals


    【解决方案1】:

    这可能是来自 Django/Python 的导入愚蠢。你需要 dispatch_uid 就像你一样,但我认为它需要成为装饰器的参数,而不是处理程序本身。

    【讨论】:

      【解决方案2】:

      我设法修复它,将信号处理移动到视图的__init__.py 而不是模型的__init__.py

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 2020-10-25
        • 1970-01-01
        • 2011-02-08
        相关资源
        最近更新 更多