【发布时间】:2017-01-27 04:40:11
【问题描述】:
我正在使用 django 1.10.5
似乎 app1 中的 apps.py 文件没有被导入,除非我在该模块的 __init__ 中明确设置 default_app_config = 'app1.apps.App1Config'。
然而,in the docs 我正在阅读“新应用程序应避免使用 default_app_config。相反,它们应要求在 INSTALLED_APPS 中显式配置相应 AppConfig 子类的虚线路径。”
我将其视为在 INSTALLED_APPS 中包含该模块,例如
INSTALLED_APPS = (
'...',
'app1',
)
我确实有。
也许我对“适当的 AppConfig 子类的虚线路径”这一语言感到困惑,也许除了列出主模块之外还有更多内容?
我的具体用途是我想导入 handlers.py 以便将其包含在应用程序中,因为它有一些需要监听的信号接收器。
为此,我遵循了in the docs 的建议,它说“实际上,信号处理程序通常在与其相关的应用程序的信号子模块中定义。信号接收器在您的应用程序配置的 ready() 方法中连接类。如果您使用的是 receiver() 装饰器,只需在 ready() 中导入信号子模块。"
# apps.py
from django.apps import AppConfig
class App1Config(AppConfig):
name = 'app1'
def ready(self):
import app1.handlers
# handlers.py
from django.dispatch import receiver
from django.db.models.signals import post_save
from app1.models import App1
@receiver(post_save, sender=App1)
def say_you_did_something(sender, instance, **kwargs):
print("Action has been taken.")
但这绝对没有任何作用......
直到我也添加了
# __init__.py
default_app_config = 'individual.apps.IndividualConfig'
除了
所以要从实际意义上重述这个问题,让项目了解 handlers.py 文件的推荐方法是什么?
【问题讨论】:
标签: django