Django 1.7升级错误:AppRegistryNotReady使用rest_framework中的序列化程序

时间:2014-10-19 07:14:03

标签: django django-1.7

我得到了这个追溯:

Traceback (most recent call last):
  File "./manage.py", line 38, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 88, in <module>
    class VersionSerializer(serializers.ModelSerializer):
  File "/Users/mgregory/Documents/cm_central/cmh_server/models.py", line 89, in VersionSerializer
    brzs= BrzSerializer(many=True)
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 198, in __init__
    self.fields = self.get_fields()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 234, in get_fields
    default_fields = self.get_default_fields()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/rest_framework/serializers.py", line 732, in get_default_fields
    reverse_rels = opts.get_all_related_objects()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 498, in get_all_related_objects
    include_proxy_eq=include_proxy_eq)]
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/db/models/options.py", line 510, in get_all_related_objects_with_model
    self._fill_related_objects_cache()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-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 "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File "/Users/mgregory/Documents/virtualenvs/cm_central/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

来自此代码(models.py):

# Serializers for transmitting CMx install information over HTTP

class BrzSerializer(serializers.ModelSerializer):
    class Meta:
        model = Brz
        fields=('filename',)

class VersionSerializer(serializers.ModelSerializer):
    brzs= BrzSerializer(many=True)
    class Meta:
        model = Version
        fields=('name', 'for_mac', 'for_windows', 'brzs')

听起来它告诉我&#34;你的VersionSerializer没有BrzSerializer,因为你还没有注册#34;。

我已经查看了与AppRegisteryNotReady相关的其他SO问题,但没有找到符合此症状的问题。当然,我必须能够像这样定义一系列依赖模型吗?

1 个答案:

答案 0 :(得分:0)

事实证明,在models.py中声明序列化程序会导致在app注册表加载完成之前使用该应用程序。

models.py实际上是声明这些序列化程序的错误位置(尽管我99%确定我是基于如何使用它们的例子那样做的。)

修复是将序列化程序的声明移到他们自己的文件中(这是有道理的,因为它们与数据库模式无关,models.py正在定义),并从视图中导入它。当视图开始运行时,应用程序注册表已准备就绪。