RuntimeError: Model class users.models.UserProfile doesn't declare an explicit app_label and is

环境

    python3.6

    django2.0

在使用django-rest-framework搭建网站中,遇到了

File "C:\Users\h\DjangoWeb\MxShop\apps\users\views.py", line 11, in <module>
  from .models import VerifyCode
File "C:\Users\h\DjangoWeb\MxShop\apps\users\models.py", line 9, in <module>
  class UserProfile(AbstractUser):
File "C:\Users\h\DjangoWeb\MxShop\venv\lib\site-packages\django\db\models\base.py", line 108, in __new__
  "INSTALLED_APPS." % (module, name)
  RuntimeError: Model class users.models.UserProfile doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

报错。

网上的解决方法由如下:

(1)引用路径不正确,因为在配置文件中我们已经告诉Django去apps下去找各个app了,所以引用各app下的模型文件时,直接from operation即可

from apps.operation.models import CourseComments, UserFavorite, UserMessage, UserCourse, UserAsk

将路径改为

from operation.models import CourseComments, UserFavorite, UserMessage, UserCourse, UserAsk

(2)在settings.py中改INSTALLED_APPS。。

(3)以上的方法我尝试了行不通,后来在apps.users.apps.py(与model.py同目录)文件中发现是我设置出错导致。

原先

from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = 'apps.users'
    verbose_name = "用户管理"

 改为

from django.apps import AppConfig


class UsersConfig(AppConfig):
    name = 'users'
    verbose_name = "用户管理"
发布了14 篇原创文章 · 获赞 3 · 访问量 614

猜你喜欢

转载自blog.csdn.net/qq_41011723/article/details/104076705