Pycharm 3.4.1-"AppRegistryNotReady:模型尚未加载".Django Rest框架 [英] Pycharm 3.4.1 - "AppRegistryNotReady: Models aren't loaded yet". Django Rest framewrok

查看:53
本文介绍了Pycharm 3.4.1-"AppRegistryNotReady:模型尚未加载".Django Rest框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DRF Pycharm 3.4.1 Django 1.7 .当我尝试通过Pycharm django控制台测试我的序列化程序类时,它给了我以下错误:

I'm using DRF and Pycharm 3.4.1 and Django 1.7. When I try to test my serializer class via Pycharm django console, it gives me the following error:

代码

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

然后导致以下错误回溯:

then cause the following error traceback:

跟踪

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 有问题,但我不知道如何解决!

but when I'm using my terminal(instead of pycharm django console), It works properly! I know there is a problem with Pycharm but I don't know how to fix it!

推荐答案

加载应用程序注册表是 django.setup 方法的一部分.如果开始使用控制台时未加载应用程序注册表,则最可能的原因是它是普通的python控制台,而不是功能齐全的Django控制台.

Loading the app registry is part of the django.setup method. If the app registry is not loaded when you start using the console, the most likely reason is that it is a plain python console instead of a fully blown Django console.

尝试以下代码.如果能够解决问题,则说明您确实在使用普通的python控制台.

Try the following code. If that solves it, you are indeed using a plain python console.

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

这篇关于Pycharm 3.4.1-"AppRegistryNotReady:模型尚未加载".Django Rest框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆