Why is post_save being raised twice during the save of a Django model?

北慕城南 提交于 2019-11-30 08:27:05
Lance McNearney

Apparently, Python is sensitive to the way you import modules. In my case, it wasn't an issue with any of import code inside my blog application but an issue with the INSTALLED_APPS configuration, which I assume is used by Django to do an initial import.

Inside my blog application I was using imports such as:

from blog.models import *

My settings.py was configured as:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    ...snip...
    'sorl.thumbnail',
    'mysite.blog',
)

The "mysite" prefix was added because I originally had import path issues when deploying the site. Later I fixed this issue (so it acted the same as the development server) by adding multiple paths in my WSGI script.

Removing the "mysite" prefix from the settings.py fixed the issue:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    ...snip...
    'sorl.thumbnail',
    'blog',
)

While looking for the root of this problem, you can use quick workaround to prevent registering signal twice:

signals.post_save.connect(my_handler, MyModel, dispatch_uid="path.to.this.module")

Source.

Here is the ticket about this issue: Django's signal framework may register listeners more than once #3951. It is now fixed in SVN version of Django.

The problem is exactly as You said: Your module which registers signal, is loaded couple of times, in some cases by different import paths, thus each imported modules this way are wrongly interpreted by Django as different modules which registers the same signal.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!