我想在我的django项目中构建一个通知系统。所以我开始创建一个名为notification的新应用程序。要创建通知,我必须听取项目其他模型的操作。为了达到这个目的,我在通知应用程序中创建了一个信号处理程序:
在notification / signals.py
中def create_subscription(sender, **kwargs):
pass
我将此处理程序连接到我的notification / apps.py
中的信号from django.apps import AppConfig
from django.db.models.signals import post_save
from notification.signals import create_subscription
from django.conf import settings
class NotificationConfig(AppConfig):
name = 'notification'
def ready(self):
post_save.connect(create_subscription, sender=settings.AUTH_USER_MODEL, dispatch_uid="create_subscription")
这很好用。我使用了在我的设置中定义的自定义用户模型。
但每当我想使用我的项目的另一个模型时,例如:
from django.apps import AppConfig
from django.db.models.signals import post_save
from notification.signals import create_subscription
from member.models import Participation
class NotificationConfig(AppConfig):
name = 'notification'
def ready(self):
post_save.connect(create_subscription, sender=Participation, dispatch_uid="create_subscription")
无论我使用哪种型号,都会收到AppRegistryNotReady错误。
我检查了我的settings.INSTALLED_APPS声明的顺序,'member'在'notification'之前声明。
通过传递throw.AUTH_USER_MODEL引用用户模型时,它工作正常,但直接引用模型时会产生错误。
有什么想法吗?
答案 0 :(得分:1)
虽然您无法在定义AppConfig类的模块级别导入模型,但您可以使用import语句或get_model()将它们导入ready()。
你需要做的事情
class NotificationConfig(AppConfig):
name = 'notification'
def ready(self):
from member.models import Participation
post_save.connect(create_subscription, sender=Participation, dispatch_uid="create_subscription")
了解更多info