当前位置:   article > 正文

django orm常见错误raise AppRegistryNotReady(“Apps aren‘t loaded yet.“)RuntimeError: Model class models等等_raise appregistrynotready("apps aren't loaded yet.

raise appregistrynotready("apps aren't loaded yet.")
错误一:ValueError: Cannot assign "<QuerySet [<User: user>]>": "SwitchRecord.user_id" must be a "User" instance.
原因:
# 外键django用户id
class SwitchRecord(models.Model):
    
    user_id = models.ForeignKey('User', on_delete=models.SET(0))
解决方法:传入User实例test_user[0]
test_user = User.objects.filter(id=8)
print(type(test_user[0]))

错误二:AttributeError: 'QuerySet' object has no attribute 'id'

原因:
User.objects.filter(id=8)返回的是QuerySet类

错误三:raise AppRegistryNotReady("Apps aren't loaded yet.")

错误原因:xxx.settings INSTALLED_APPS配置有误,错误如下图

 

去除xxx.models,django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.报错解决

错误四

解决错误三后又报错:RuntimeError: Model class models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

原因:使用相对路径导包
from models import User
解决方法:使用绝对路径导包
from xxx.models import User
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号