IT

Django 1.7에서 django.core.exceptions.AppRegistryNotReady가 발생 함 : 모델이 아직로드되지 않았습니다

lottoking 2020. 5. 31. 10:30
반응형

Django 1.7에서 django.core.exceptions.AppRegistryNotReady가 발생 함 : 모델이 아직로드되지 않았습니다


이것은 내 Windows 시스템의 역 추적입니다.

Traceback (most recent call last):
  File "D:\AMD\workspace\steelrumors\manage.py", line 9, in <module>
    django.setup()
  File "D:\AMD\Django\django-django-4c85a0d\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Python27\lib\site-packages\registration\models.py", line 15, in <module>
    User = get_user_model()
  File "D:\AMD\Django\django-django-4c85a0d\django\contrib\auth\__init__.py", line 135, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\registry.py", line 199, in get_model
    self.check_models_ready()
  File "D:\AMD\Django\django-django-4c85a0d\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.

그리고 내 manage.py는 다음과 같습니다.

import os
import sys
import django

if __name__ == "__main__":

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "steelrumors.settings")
    django.setup()
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

Django 1.7에서 등록을 사용하려고 할 때이 오류가 발생합니다


이것이 우리 와이 사람들을 위해 그것을 해결 한 것입니다 .

우리 프로젝트는 Django 1.4로 시작하여 1.5로, 1.7로갔습니다. 우리 wsgi.py는 다음과 같았습니다 :

import os

from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = WSGIHandler()

1.7 스타일 WSGI 핸들러로 업데이트했을 때 :

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

모든 것이 지금 작동합니다.


이 명령을 실행하면 내 문제가 해결되었습니다 ( 이 답변에 대한 신용 ) :

import django
django.setup()

그러나 왜 이것이 필요한지 잘 모르겠습니다. 의견을 부탁드립니다.


The issue is in your registration app. It seems django-registration calls get_user_module() in models.py at a module level (when models are still being loaded by the application registration process). This will no longer work:

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User    

I'd change this models file to only call get_user_model() inside methods (and not at module level) and in FKs use something like:

user = ForeignKey(settings.AUTH_USER_MODEL)

BTW, the call to django.setup() shouldn't be required in your manage.py file, it's called for you in execute_from_command_line. (source)


Just encountered the same issue. The problem is because of django-registration incompatible with django 1.7 user model.

A simple fix is to change these lines of code, at your installed django-registration module::

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User  

to::

from django.conf import settings
try:
    from django.contrib.auth import get_user_model
    User = settings.AUTH_USER_MODEL
except ImportError:
    from django.contrib.auth.models import User 

Mine is at .venv/local/lib/python2.7/site-packages/registration/models.py (virtualenv)


This works for me for Django 1.9 . The Python script to execute was in the root of the Django project.

    import django 
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_NAME.settings")
    django.setup()
    from APP_NAME.models import *

Set PROJECT_NAME and APP_NAME to yours


Another option is that you have a duplicate entry in INSTALLED_APPS. That threw this error for two different apps I tested. Apparently it's not something Django checks for, but then who's silly enough to put the same app in the list twice. Me, that's who.


Do you have a Python virtual environment that you need to enter before you run manage.py?

I ran into this error myself, and that was the problem.


I ran into this issue when I use djangocms and added a plugin (in my case: djangocms-cascade). Of course I had to add the plugin to the INSTALLED_APPS. But the order is here important.

To place 'cmsplugin_cascade' before 'cms' solved the issue.


install django-registration-redux==1.1 instead django-registration, if you using django 1.7


./manage.py migrate

This solved my issue


Your manage.py is "wrong"; I don't know where you got it from, but that's not a 1.7 manage.py - were you using some funky pre-release build or something?

Reset your manage.py to the conventional, as below, and things Should Just Work:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

참고URL : https://stackoverflow.com/questions/25537905/django-1-7-throws-django-core-exceptions-appregistrynotready-models-arent-load

반응형