[Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet

61๐Ÿ‘

โœ…

This is what solved it for us and these folks:

Our project started with Django 1.4, we went to 1.5 and then to 1.7. Our wsgi.py looked like this:

import os

from django.core.handlers.wsgi import WSGIHandler

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

When I updated to the 1.7 style WSGI handler:

import os

from django.core.wsgi import get_wsgi_application

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

Everything works now.

๐Ÿ‘คNick Spacek

239๐Ÿ‘

Running these commands solved my problem (credit to this answer):

import django
django.setup()

However Iโ€™m not sure why I need this. Comments would be appreciated.

๐Ÿ‘คNimo

58๐Ÿ‘

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)

๐Ÿ‘คgonz

19๐Ÿ‘

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)

๐Ÿ‘คhails

14๐Ÿ‘

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

๐Ÿ‘คLorenzo Lerate

5๐Ÿ‘

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.

๐Ÿ‘คMark

3๐Ÿ‘

Iโ€™m damn sure this is isnโ€™t late. If you are using Django 4 and .env file for your settings, you are going to encounter this error if you define a value in settings.py while that value does not exist in .env file:

See following scenario:

I had PLAID_KEY in my settings.py as follows

PLAID_KEY=env('PLAID_KEY')

However, PLAID_KEY did not exist in my .env file, adding it fixed it.

2๐Ÿ‘

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.

๐Ÿ‘คTravis

2๐Ÿ‘

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.

๐Ÿ‘คpabo

2๐Ÿ‘

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

๐Ÿ‘คuser2350206

0๐Ÿ‘

./manage.py migrate

This solved my issue

๐Ÿ‘คOmar Natour

0๐Ÿ‘

If you get this error in a context of creating ForeignKey relations between models. Example below raises AppRegistryNotReady: Models aren't loaded yet error.

from my_app.models import Workspace

workspace = models.ForeignKey(Workspace)

Then please try to reffer to a model as a string.

from my_app.models import Workspace

# One of these two lines might fix the problem.
workspace = models.ForeignKey('Workspace')
workspace = models.ForeignKey('my_app.Workspace')
๐Ÿ‘คLukasz Dynowski

-1๐Ÿ‘

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)
๐Ÿ‘คKristian Glass

-1๐Ÿ‘

My Problem was from init.py . i made an app and wanted to do this :

from MY_APP import myfunc

instead of :

from MY_APP.views import myfunc

when i rolled back my changes to these parts . everything worked just fine.

๐Ÿ‘คSomeone

Leave a comment