我一直在尝试在中间件进程函数中导入模型,但出现错误:
Value: Model class x doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
我猜问题是应用程序设置不完整,但是,我解决这个问题的所有尝试都碰壁了。
中间件.py 函数
from notifications.models import Notification
...
class CheckNotificationMiddleware(object):
"""
Checks clicked notification to mark it as read
"""
def process_request(self, request):
if request.user.is_authenticated():
notification_id = request.GET.get('notification_id')
if notification_id:
AppConfig.get_model('Notification')
try:
notification = Notification.objects.get(
pk=notification_id, status=0, recipient=request.user)
notification.status = 1
notification.save()
except ObjectDoesNotExist:
pass
设置.py
INSTALLED_APPS = (
...
'notifications',
...
)
我尝试了几件事,包括...
from django.apps import AppConfig
AppConfig.get_model('Notification')
我正在使用 Django 1.11 版和 python 2.7