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

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()

关于 run

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 with Django本书中的教程,但由于这本书涉及Django 1.5.4,我使用的是Django 1.8,所以这里有人能帮我吗?

推荐答案

对于Django 1.7rc2,我也有同样的例外.解决方案是在我程序的开头添加以下几行:

import django
django.setup()

更新:This is now documented for Django 1.8.

Django相关问答推荐

Django显示交叉表格

Django-无法显示基于字段值的元素

我希望用户能够预订相同的桌子,但不是在同一时间

如何在 Fargate 容器中运行的 Django 中使用 AWS SES?

无法创建超级用户,因为 Django 中的一列(外键)不能为空

Pymongo:搜索文档时忽略空输入值

如何在Django中制作一个不 Select 以前日期的日期 Select 器

来自 Django 应用程序的日志(log)消息未上传到 AWS CloudWatch

如何根据查询集中的条件返回多个聚合?

如何在 django 中处理这种竞争条件?

Django:创建索引:非唯一,多列

django.core.exceptions.ImproperlyConfigured:请求设置 USE_I18N,但未配置设置

使用 sqlite 运行 django 测试

UnicodeEncodeError:ascii编解码器无法编码字符

Django 和 VirtualEnv 开发/部署最佳实践

Django:验证上传文件的文件类型

暂时禁用 Django 缓存

如何更改模型标签并给它一个自定义名称

何时使用 Django get_absolute_url() 方法?

ImportError:没有名为 django_filters 的模块