当前位置: 移动技术网 > IT编程>脚本编程>Python > Django开发学习BUG记录--RemovedInDjango19Warning:Model class apps.user.models.User doesn't declare an explicit app_label

Django开发学习BUG记录--RemovedInDjango19Warning:Model class apps.user.models.User doesn't declare an explicit app_label

2019年10月16日  | 移动技术网IT编程  | 我要评论

有家足矣,富贵吉娃娃2,李维汉子女

报错信息:

/home/python/pycharmprojects/dailyfresh/apps/user/models.py:8: removedindjango19warning: model class apps.user.models.user doesn't declare an explicit app_label and either isn't in an application in installed_apps or else was imported before its application was loaded. this will no longer be supported in django 1.9.
  • 报错解析:apps.user.models 没有声明

原因:当时创建项目时为了简化注册app的写法,所以在setting中加入了apps的路径

sys.path.insert(0, os.path.join(base_dir,'apps'))

然后注册app时就不用apps.

installed_apps = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tinymce',
    'cart',
    'goods',
    'order',
    'user',
)

但是当在user.views导入user.models时直接导入

from user.models import user

按这样导入pycharm会有红色波浪线提示,所以又改回原始写法

from apps.user.models import user

这样开启服务器后就会出现上面的报错

这样就会导致django注册时app为user,但导入使用时是apps.user,所以django没有找到对应声明的app所以报错

  • 解决方法:

方法1.忽略pycharm报错,还是直接导入

 

from user.models import user

 

方法2:注册app时不简化写法时用apps.注册

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网