pycharm 用Django创建数据脚本,
一直报错django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet;
File "populate_rango.py", line 5, in
from rango.models import Category, Page
File "D:\WorkSpace\tango_with_django_project\rango\models.py", line 3, in
class Category(models.Model):
File "D:\WorkSpace\venv\lib\site-packages\django\db\models\base.py", line 108, in __new__
app_config = apps.get_containing_app_config(module)
File "D:\WorkSpace\venv\lib\site-packages\django\apps\registry.py", line 253, in get_containing_app_config
self.check_apps_ready()
File "D:\WorkSpace\venv\lib\site-packages\django\apps\registry.py", line 136, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
运行主函数,会报没有默认数据库配置的错误。
django.core.exceptions.ImproperlyConfigured: You must define a 'default' database.
提示:这里填写问题的分析:
网上寻找解决方案,也没有找到合适的,基本都是设置环境变量,
os.environ.setdefault('DJANGO_SETTINGS_MODULE','项目名settings')
项目名要替换成自己的项目名称
然后或者加django.setup()
代码里都有,但是依然报错,来来回回折腾了两个小时
搜索到“绵绵挂的1”的文章,发现报错是未加载程序,猜想是不是Django.setup()位置放错了
修改后: