Model class apps.goods.models.GoodsType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
阅读原文时间:2023年07月09日阅读:1

在admin.py注册这个model时,报了个错:

RuntimeError: Model class apps.goods.models.GoodsType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

admin是这样写的:

from django.contrib import admin
from apps.goods.models import GoodsType

Register your models here.

admin.site.register(GoodsType)

已经把多个应用放到一个文件夹apps,然后在settings也有注册

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tinymce', # 富文本编辑器
'user', # 用户模块
'goods', # 商品模块
'cart', # 购物车模块
'order', # 订单模块

]

apps的路径也添加进来了

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

然后仔细看admin的写法,感觉不太对,把apps去掉就正常了,因为在settings已经添加过这个路径了,所以此时只要从具体的app名导入就好

from django.contrib import admin
from goods.models import GoodsType

Register your models here.

admin.site.register(GoodsType)