python - Pycharm 3.4.1 - "AppRegistryNotReady: Models aren' t 已加载。Django Rest 框架

标签 python django pycharm

我正在使用 DRFPycharm 3.4.1 以及 Django 1.7。当我尝试通过 Pycharm django 控制台测试我的序列化程序类时,出现以下错误:

代码

from items_app.serializers import ItemSerializer
s = ItemSerializer()
print(repr(s))

然后导致如下错误回溯:

回溯

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 440, in __repr__
    return unicode_to_repr(representation.serializer_repr(self, indent=1))
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/representation.py", line 75, in serializer_repr
    fields = serializer.fields
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 312, in fields
    for key, value in self.get_fields().items():
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/serializers.py", line 883, in get_fields
    info = model_meta.get_field_info(model)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/model_meta.py", line 68, in get_field_info
    reverse_relations = _get_reverse_relationships(opts)
  File "/usr/local/lib/python2.7/dist-packages/rest_framework/utils/model_meta.py", line 129, in _get_reverse_relationships
    for relation in opts.get_all_related_objects():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 498, in get_all_related_objects
    include_proxy_eq=include_proxy_eq)]
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_model
    self._fill_related_objects_cache()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/options.py", line 533, in _fill_related_objects_cache
    for klass in self.apps.get_models(include_auto_created=True):
  File "/usr/local/lib/python2.7/dist-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
AppRegistryNotReady: Models aren't loaded yet.

但是当我使用我的终端(而不是 pycharm django 控制台)时,它工作正常!我知道 Pycharm 有问题,但我不知道如何解决!

最佳答案

加载应用注册表是 django.setup 方法的一部分。如果在您开始使用控制台时未加载应用注册表,最可能的原因是它是一个普通的 python 控制台,而不是一个完全成熟的 Django 控制台。

试试下面的代码。如果这样可以解决问题,那么您确实在使用普通的 Python 控制台。

>>> import django
>>> django.setup()

关于python - Pycharm 3.4.1 - "AppRegistryNotReady: Models aren' t 已加载。Django Rest 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28840071/

相关文章:

python - 如何在 python 中检查 Unicode 字符

python - 为什么 cqlsh 失败并出现 LookupError : unknown encoding?

python - Snakemake 扩展没有输入文件

python - 如何更改pycharm命令将rst转换为html

python - 在 Python 中导入本地模块 - ImportError : Not module named *

python - 将值列表插入 PostgreSQL

mysql - 将 Django 项目连接到远程 Mysql 数据库

html - 如何在 django 管理页面上创建 anchor 以自动从 url 向下滚动到内联或字段

python - 如何在 Django 中使用带有 crispy-forms 的 CreateView

python - 无法使用pycharm调试 flask 应用程序