Django :一个AppRegistryNotReady在尝试连接不同应用程序之间的信号时引发错误



我想在我的django项目中构建一个通知系统。所以我开始创建一个名为通知的新应用程序。要创建通知,我必须侦听项目中其他模型的操作。为了达到这个目的,我在通知应用程序中创建了一个信号处理程序:

在通知/信号中.py

def create_subscription(sender, **kwargs):
pass

我将此处理程序连接到通知/应用程序中的信号.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错误。

我检查了设置的声明顺序。INSTALLED_APPS,"会员"在"通知"之前声明。

当通过传递引用用户模型时,抛出设置。AUTH_USER_MODEL它工作正常,但是当直接引用模型时,它会产生错误。

有什么想法吗?

虽然不能在定义 AppConfig 类的模块级别导入模型,但可以使用导入语句或 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")

欲了解更多信息

相关内容

最新更新