Reputation: 2348
I'm hitting the common "Models aren't loaded yet" error that arises from having a separate signals file that is imported into my apps models.py
. I found 2 easy guides on how to create an AppConfig and import signals in the ready() function, but I can't get it working. I'm using the top answer here as well as this blog post.
Part of this is likely due to the fact that my entire project has a name, funproj
, and the main app where the bulk of the code is located is helpfully called app
- I'm using Visual Studio with Python Tools for VS and it generated this structure when I created the project. Here's the hierarchy:
funproj
|--- __init__.py
|--- setttings.py
|--- urls.py
|
|--app
| |--- __init__.py
| |--- models.py
| |--- signals.py
|
|--otherapp
| |--- __init__.py
| |--- other stuff
This is abbreviated, but I've tried putting the required apps.py
and relevant code for __init.py__
under both funproj
and app
but neither method works. This is what I have for each of those:
apps.py
from django.apps import AppConfig
class FunProjAppConfig(AppConfig):
name = 'app'
def ready(self):
from app import signals
__init.py__ addition
#import signals in the app config
default_app_config = 'app.apps.FunProjAppConfig'
It makes more sense to me to have this code in app
since it contains the signals that need to be loaded, however the examples I've linked aren't clear to me. One mentions the 'app directory' but I'm not sure if that literally means the app
, as I think it should, or if they're using app as a synonym for project.
I don't think this is going to be hard to fix, I just can't figure out what I'm doing wrong.
EDIT: This is Python 3.4 with Django 1.8. Traceback below, it's the same error I get when I simply have a signals.py
file and an import in `models.py' of those signals.
C:\Python34\lib\importlib\_bootstrap.py:321: RemovedInDjango19Warning: The utili
ties in django.db.models.loading are deprecated in favor of the new application
loading system.
return f(*args, **kwds)
Traceback (most recent call last):
File "C:\Users\David\Source\Repos\path\to\projfiles\manage.py", line
17, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
312, in execute
django.setup()
File "C:\Python34\lib\site-packages\django\__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python34\lib\site-packages\django\apps\registry.py", line 108, in pop
ulate
app_config.import_models(all_models)
File "C:\Python34\lib\site-packages\django\apps\config.py", line 198, in impor
t_models
self.models_module = import_module(models_module_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Users\David\Source\Repos\path\to\projfiles\app\models.py", l
ine 11, in <module>
from app import signals
File "C:\Users\David\Source\Repos\path\to\projfiles\app\signals.py",
line 7, in <module>
@receiver(post_save, sender=get_model('app', 'Game'))
File "C:\Python34\lib\site-packages\django\apps\registry.py", line 199, in get
_model
self.check_models_ready()
File "C:\Python34\lib\site-packages\django\apps\registry.py", line 131, in che
ck_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
EDIT2: I added a print
to the ready()
function but saw no output. Here are my installed apps:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'kombu.transport.django',
'accounts',
'app',
'scores',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Upvotes: 3
Views: 3821
Reputation: 10699
It looks like you have both apps.py
and models.py
importing your signals. When models.py
is loaded it imports signals
which then tries to reference your Game
model, causing the "Models aren't yet loaded" error.
Remove the from app import signals
from models.py
. You can import your models
from signals
but not the other way around.
Upvotes: 5