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

前端 未结 3 990
悲&欢浪女
悲&欢浪女 2020-12-31 04:17

I am attaching a method to the post_save signal of my Django model. This way I can clear some cached items whenever the model is modified.

The problem I am having i

3条回答
  •  轮回少年
    2020-12-31 05:02

    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',
    )
    

提交回复
热议问题