在Django 1.8或更高版本中填充时,出现“尚未加载模型”错误

土生土长:

我正在使用以下代码填充数据库:

import os
def populate():
    python_cat = add_cat('Python')

    add_page(cat=python_cat, 
    title="Official Python Tutorial",
    url="http://docs.python.org/2/tutorial/")

    add_page(cat=python_cat,
    title="How to Think like a Computer Scientist",
    url="http://www.greenteapress.com/thinkpython/")

    add_page(cat=python_cat,
    title="Learn Python in 10 minutes",
    url="http://www.korokithakis.net/tutorials/python/")

    django_cat = add_cat(name="Django")

    add_page(cat=django_cat,
    title="Official Django Tutorial",
    url="http://djangoproject.com/en/1.5/intro/tutorial01/")

    add_page(cat=django_cat,
    title="Django Rocks",
    url="http://www.djangorocks.com/")

    add_page(cat=django_cat,
    title="How to Tango with Django",
    url="htttp://www.tangowithdjango.com/")

    frame_cat = add_cat(name="Other Frameworks")

    add_page(cat=frame_cat, 
    title="Bottle",
    url="http://bottlepy.org/docs/dev/")

    add_page(cat=frame_cat, 
    title="Flask",
    url="http://flask.pocoo.org")

    # Print out what we have added to the user. 
    for c in Category.objects.all():
        for p in Page.objects.filter(category=c):
            print "- {0} - {1}".format(str(c), str(p))

def add_page(cat, title, url, views=0):
    p = Page.objects.get_or_create(category=cat, title=title, url=url, views=views)[0]
    return p

def add_cat(name):
    c = Category.objects.get_or_create(name=name)
    return c

if __name__ == '__main__':
    print "Starting Rango population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'p.settings')
    from rango.models import Category, Page
    populate()

跑步时

python c:\python27\p\populate_rango.py

它给出了错误:

Staring Rango population script...
Traceback (most recent call last):
File "c:\python27\p\populate_rango.py", line 59, in <module>
populate()
File "c:\python27\p\populate_rango.py", line 4, in populate
python_cat = add_cat('Python')
File "c:\python27\p\populate_rango.py", line 52, in add_cat
c = Category.objects.get_or_create(name=name)
File "C:\Python27\Lib\site-packages\django\db\models\manager.py", li
manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python27\Lib\site-packages\django\db\models\query.py", line
et_or_create
return self.get(**lookup), False
File "C:\Python27\Lib\site-packages\django\db\models\query.py", line
clone = self.filter(*args, **kwargs)
File "C:\Python27\Lib\site-packages\django\db\models\query.py", line
ilter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python27\Lib\site-packages\django\db\models\query.py", line
filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py",
in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py",
in _add_q
current_negated=current_negated, connector=connector)
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py",
in build_filter
lookups, parts, reffed_aggregate = self.solve_lookup_type(arg)
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py",
in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, se
a())
File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py",
in names_to_path
field, model, direct, m2m = opts.get_field_by_name(name)
File "C:\Python27\Lib\site-packages\django\db\models\options.py", li
get_field_by_name
cache = self.init_name_map()
File "C:\Python27\Lib\site-packages\django\db\models\options.py", li
init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "C:\Python27\Lib\site-packages\django\db\models\options.py", li
get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "C:\Python27\Lib\site-packages\django\db\models\options.py", li
_fill_related_many_to_many_cache
for klass in self.apps.get_models():
File "C:\Python27\Lib\site-packages\django\utils\lru_cache.py", line
rapper
result = user_function(*args, **kwds)
File "C:\Python27\Lib\site-packages\django\apps\registry.py", line 1
_models
*self.check_models_ready()
File "C:\Python27\Lib\site-packages\django\apps\registry.py", line 1
ck_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.*

我的其余文件都可以,但是出现此错误。我正在遵循Tango的Django教程,但由于该书涉及Django 1.5.4,而我正在使用Django 1.8,所以有人可以在这里帮助我吗?

pdw:

我在Django 1.7rc2中有同样的例外。解决方案是在程序的开头添加以下行:

import django
django.setup()

更新:现在已针对Django 1.8进行了记录。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用django-mysql修复Django中尚未加载的应用尚未加载的错误?

为什么当我尝试在celery任务中使用模型时django引发“尚未加载应用程序”错误?

django导入导出应用程序错误:django.core.exceptions.AppRegistryNotReady:尚未加载模型

无法在djongo中创建EmbeddedField并收到此错误django.core.exceptions.AppRegistryNotReady:尚未加载模型

的Django 2模型1

Django 1.7升级错误:AppRegistryNotReady:模型尚未加载

django.core.exceptions.AppRegistryNotReady:模型尚未加载

Django 应用程序尚未加载:如何导入模型

带有Djongo的Django,EmbeddedFields返回模型尚未加载

尚未加载Django应用

使用asgi时,尚未加载Django应用

在python Django中,我想查询表的foreignKey列,它引发django.core.exceptions.AppRegistryNotReady:模型尚未加载

Django 1.7抛出django.core.exceptions.AppRegistryNotReady:模型尚未加载

Django + tox:尚未加载应用

AppRegistryNotReady:应用尚未加载。Django的

Django 2.0 迁移错误:“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载。”

Pycharm 3.4.1-“ AppRegistryNotReady:模型尚未加载”。Django Rest框架

在Django中“尚未加载应用”和“ django.core.exceptions.ImproperlyConfigured”?

django:django.core.exceptions.AppRegistryNotReady:应用尚未加载

Django 1 到 Django 2 on_delete 错误

Django ORM bulk_create 1:1 相关模型

测试Django中的ForeignKey的重复条目“ 1”

收到错误消息:django.core.exceptions.AppRegistryNotReady:在Django Rest框架中安装oauth2提供程序时尚未加载应用程序

Django升级到1.9错误“ AppRegistryNotReady:应用尚未加载。”

无法修复错误“django.core.exceptions.AppRegistryNotReady:应用程序尚未加载。”

尝试运行pytest-django时显示“应用尚未加载”

VS2015 Django Shell中的Django 1.9.7 django.core.exceptions.AppRegistryNotReady:应用尚未加载

Django:未加载用户用户模型

在Django 1.7中向1模型添加新字段时出错