当前位置: 首页 > 面试题库 >

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

翟曦之
2023-03-14
问题内容

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

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,所以有人可以在这里帮助我吗?


问题答案:

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

import django
django.setup()

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



 类似资料:
  • 问题内容: 我正在用Django开发应用程序。 我想将数据加载到模型中,即,但数据存储在xlsx文件中,即。 为了实现这一目标,我开发了以下脚本: 但是当我从Anaconda提示符下运行它时,我得到了 文件“ load_glossary.py”,模块7中的第7行, 引发AppRegistryNotReady(“应用尚未加载。”)django.core.exceptions.AppRegistryN

  • 问题内容: 这是Windows系统上的追溯。 我的manage.py看起来像这样: 当我尝试在Django 1.7中使用注册应用程序时出现此错误 问题答案: 这就是为我们和这些人解决的问题: 我们的项目从Django 1.4开始,然后转到1.5,然后到1.7。我们的wsgi.py看起来像这样: 当我更新到1.7样式的WSGI处理程序时: 现在一切正常。

  • 问题内容: 我正在尝试将项目从Django 1.6升级到1.7。到目前为止,我已经使用相同的安装创建了一个新的virtualenv,并将Django版本升级到了新版本。我需要从南方升级,但是这样做有错误,所以我认为我最初只是尝试运行服务器,然后出现以下错误: 有什么想法可能导致错误,以及如何解决该错误? 问题答案: 问题在于此行(“ /Users/Name/Dev/tps/products/mod

  • 我想每个人都在Windows中看到了加载栏,在Windows vista,7和8中,我们这里有一些有趣的东西,你可以在任务栏上看到绿色的加载图标。如何创建与此工作的加载?我是新手,我创建了一个文本窗口,其中程序是打印空格与绿色背景,这看起来类似加载栏。我不知道如何创造更好的装载。。。

  • 我试图在Protege3.5中加载一个OWL2.0本体。然而,每次我尝试的时候,它都会给出一个错误。 本体很好,因为当我从Java代码运行它们时,Hermit Reasoner和Pellet Reasoner在它上面工作得很好。你能告诉我为什么会出现错误信息以及如何修复它吗。

  • 嘿,朋友们,我是djongo的新手,在练习它的用法时,我不断地遇到这个错误 文件“D:\python\django Rest-webs\tms-Rest-API\BACKEND\bckend\students\models.py”,第6行,课堂学生(models.Model):文件“D:\python\django Rest-webs\tms-Rest-API\BACKEND\bckend\stu