170

This is the traceback on my windows system.

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.

And my manage.py looks like this:

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)

I get this error when i am trying to use registration app in Django 1.7

slamora
  • 697
  • 10
  • 17
doubleo
  • 4,389
  • 4
  • 16
  • 19
  • 1
    You have a directory called `django-django-4c85a0d`; `4c85a0d` happens to be a (non-stable) Django commit hash. I don't believe you're actually using Django 1.7 (c.f. my answer) – Kristian Glass Sep 06 '14 at 11:14
  • Did you ever resolve your issue? – Nick Spacek Dec 12 '14 at 16:56
  • 1
    This could also related to `venv`. Recreating the venv directory fixed it for me. ``` mv venv venv_old virtualenv venv source ./venv/bin/activate pip install -r requirements.txt ``` – Thomas - BeeDesk May 25 '15 at 20:05
  • 1
    I also meet some error like this when i add LOGGING in settings.py on my mac,this because i create a log file on /var/log/xx/debug.log and don't have the permission,so i use sudo to runserver,and everthing is fine.May help some gays – gkiwi Jan 12 '16 at 04:26
  • I saw this error when running `docker-compose exec ...`. The issue was that I wasn't passing required environment variables to the command [docker-compose exec](https://docs.docker.com/compose/reference/exec/). – Matthew Hegarty Jul 30 '19 at 09:27

14 Answers14

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.

Community
  • 1
  • 1
Nimo
  • 7,984
  • 5
  • 39
  • 41
  • 13
    Sorry, just parroting what I've read on the django 1.7 release notes about breaking changes. https://docs.djangoproject.com/en/dev/releases/1.7/#backwards-incompatible-changes-in-1-7. Basically, Django has a new way to load installed app. If you load Django from a Python script (like I was in my custom unit tests), some initialization needs to be done before proceeding and calling setup() is how to do it. Aside from that, kudos to the team, my 1.6.2 to 1.7.1 upgrade seems to an hour's worth of real work. – JL Peyret Oct 30 '14 at 18:10
  • 13
    Where do I run the above command? I add it to a .py file or what? – KhoPhi Nov 24 '14 at 20:20
  • 1
    You should run this in the same context that the error occurs – Nimo Nov 27 '14 at 15:44
  • 3
    this has enough upvotes to merit being marked the correct answer – acid_crucifix Mar 01 '15 at 20:06
  • This also solved a problem I had in a command-line update script that broke in the move to 1.7. – Jason Champion Jul 30 '15 at 01:13
  • If you're building a standalone, you need the django.setup() call: https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage The other workarounds (get_wsgi_application(), etc.) call django.setup() or something similar in the background. – Sarah Messer Aug 17 '15 at 20:32
  • I'm upgrading from 1.4, and this solution did not work while the accepted answer did. Just adding signal for others. – Michael Terry Nov 14 '16 at 21:57
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.

Community
  • 1
  • 1
Nick Spacek
  • 4,717
  • 4
  • 39
  • 42
  • 1
    I don't agree with the edit from @wim, not because I think we should leave the unnecessary import, but because I was giving an example of what the autogenerated wsgi.py looked like before, and what it looked like after I had copied the differences from the 1.7 autogenerated wsgi.py. – Nick Spacek Oct 11 '16 at 12:24
  • 1
    The autogenerated `wsgi.py` file never has an `import sys` in it. Not in 1.4, not in 1.5, and not in 1.7. If you had it in there, it was added manually by someone - it is not added by django-admin startproject. – wim Oct 11 '16 at 16:54
  • Good to know, my mistake (and poor memory). It had been a while since I made this but I was pretty sure that at the time I was trying to document the built in behavior. – Nick Spacek Oct 11 '16 at 17:23
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
  • 5,226
  • 5
  • 39
  • 54
  • putting it inside ```if __name__ == '__main__':``` works for me but I don't know if it's a good solution. – Umair A. Oct 12 '14 at 20:35
  • @Neutralizer Not sure how are you doing that, but that shouldn't work since django is importing this module. Probably you're avoiding the circular dep by not importing the User model at all. – gonz Oct 13 '14 at 18:08
  • 1
    I mean put those lines inside name check. It might be skipping the execution. I have not made enough tests. – Umair A. Oct 14 '14 at 13:42
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
  • 199
  • 1
  • 2
  • 7
    You can use django-registration-redux instead. It's updated and maintained fork: https://github.com/macropin/django-registration – TJL Feb 02 '15 at 20:38
  • 1
    [`django-registration-redux`](https://django-registration-redux.readthedocs.org/en/latest/) fixed the issue for me (I had exactly the same stack as the OP) – Pierre de LESPINAY Mar 28 '15 at 08:14
  • 1
    In case anyone was struggling with this on Django 1.8, it also applies there as well. – Andrew Schuster Apr 28 '15 at 02:44
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
  • 3,552
  • 3
  • 26
  • 25
  • 1
    It works for me. but I don't know why we should run this code as we already mentioned these in our wsgi file. – Mr Code May 31 '18 at 01:00
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
  • 18,730
  • 7
  • 107
  • 130
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
  • 1,998
  • 1
  • 21
  • 36
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
  • 808
  • 7
  • 14
2

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

user2350206
  • 185
  • 8
0

./manage.py migrate

This solved my issue

Omar Natour
  • 101
  • 10
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/24014876) – George Z. Sep 10 '19 at 19:44
  • @GeorgeZ. It looks like an attempt to answer the question, and apparently solved the problem when this poster ran into it - even if you think it's wrong or should be elaborated more, a genuine attempt to answer the question shouldn't be deleted from review – CertainPerformance Sep 10 '19 at 21:27
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
  • 11,169
  • 9
  • 81
  • 124
-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
  • 37,325
  • 7
  • 45
  • 73
-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.