Search code examples
djangodjango-1.7

Django 1.7 upgrade error: AppRegistryNotReady using serializers from rest_framework


I get this traceback:

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.

from this code (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')

It sounds like it's telling me "your VersionSerializer can't have a BrzSerializer, because you haven't registered that yet".

I've looked at other SO questions relating to AppRegisteryNotReady, but didn't find one that matches this symptom. Surely I have to be able to define a chain of dependent models like this?


Solution

  • It turns out that having the declaration of the serializers inside models.py is causing that application to be used before the app registry has finished being loaded.

    models.py is actually the wrong place to declare these serializers (though I'm 99% certain I did it that way based on an example of how to use them).

    The fix is to move the declaration of the serializers out into their own file (which makes sense, because they have nothing to do with the database schema, which models.py is defining), and import that from the view. By the time the view gets going, the app registry is ready.