Pycharm 3.4.1 - "AppRegistryNotReady:模型尚未加载".Django Rest framewrok

Had*_*adi 3 python django pycharm

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

from items_app.serializers import ItemSerializer
s = ItemSerializer()
print(repr(s))
Run Code Online (Sandbox Code Playgroud)

然后导致以下错误回溯:

追溯

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.
Run Code Online (Sandbox Code Playgroud)

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

knb*_*nbk 5

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

请尝试以下代码.如果这解决了它,你确实使用普通的python控制台.

>>> import django
>>> django.setup()
Run Code Online (Sandbox Code Playgroud)