14

I am trying to create a script that populates a database with test users. I am new to Django and Python. I keep on getting:

Runtime error: App registry isn't ready yet. 

Here is the output and error:

starting population script
Traceback (most recent call last):
File "populate.py", line 32, in <module>
populate()
File "populate.py", line 22, in populate
i.save()
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\db\models\base.py", line 603, in save
force_update=force_update, update_fields=update_fields)
...
...
... 
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\apps\registry.py", line 156, in get_models
self.check_ready()
File "c:\Python27\lib\site-packages\django-1.7a2-py2.7.egg\django\apps\registry.py", line 119, in check_ready
raise RuntimeError("App registry isn't ready yet.")
RuntimeError: App registry isn't ready yet.

Here is the code:

import os
import datetime
def populate():
 freer = User.objects.create_user( 'joyyie', '[email protected]', 'e')
 cat = User.objects.create_user( 'steve', '[email protected]', 'e')
 dog = User.objects.create_user( 'aasd', '[email protected]', 'ad')
 cow = User.objects.create_user( 'sadsfa', '[email protected]', 't' )
 pig = User.objects.create_user( 'regibald', '[email protected]', '0')
 donkey = User.objects.create_user( 'turnip', '[email protected]', 'pop')
 human = User.objects.create_user( 'tutu', '[email protected]', 'pa')
 a = [freer,cat,dog,cow,pig,donkey,human]
 for i in a:
 i.first_name= 'jackee'
 i.is_superuser=True
 i.is_staff=False
 i.date_joined=datetime.datetime.today()
 i.last_login=datetime.datetime.today()
 i.save()
if __name__=='__main__':
 print "starting population script"
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'infosmos.settings')
 from django.conf import settings
 from django.db import models
 from django.contrib.auth.models import User
 populate()

Is there a way to force the user profile creation to wait for the registry app by using a signal or something?

P̲̳x͓L̳
3,6613 gold badges31 silver badges37 bronze badges
asked Mar 30, 2014 at 14:07

4 Answers 4

21

[django 1.7] The Standalone script you wish to run, import django and settings like below

import os
import django
[...]
if __name__ == '__main__': 
 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
 django.setup()
zaius
6,4194 gold badges30 silver badges50 bronze badges
answered Apr 23, 2014 at 10:05
Sign up to request clarification or add additional context in comments.

1 Comment

still a perfect solution to set default environment variable and setup app to resolve app registry problem.
5

This is a known and intended behaviour according to Django's 1.7 release notes, under the "startup sequence" clause:

Another common culprit is django.contrib.auth.get_user_model(). Use the AUTH_USER_MODEL setting to reference the User model at import time.

and that should do the trick

for reference: https://docs.djangoproject.com/en/dev/releases/1.7/#app-loading-changes

answered Mar 30, 2014 at 14:13

1 Comment

I don't see django.contrib.auth.get_user_model used in the above code, and can't imagine how anything in that code could make use of the AUTH_USER_MODEL setting. To me the problem seems more likely with the missing django.setup() call.
2

I found out that if I run populate through the manage.py shell with the execfile() command then it runs properly. Everything needed to be setup before I start modifying the database or run outside code. Thanks to lanzz for the hint.

answered Mar 31, 2014 at 3:00

1 Comment

You might want to make this a management command docs.djangoproject.com/en/1.7/howto/custom-management-commands
0

If all other solutions didn't work for you (like I was), and you are sure that you are not accessing the database at import time, then its highly possible that the problem is related to some of incompatible or missing dependencies and the "AppRegistryNotReady" exception is just a fallback. For me, I followed the exception's trace back, one-by-one until I finally knew what the actual problem was! You may need to go to the .../django/core/management/__init__.py and search for the method execute in the class ManagementUtility, you will find the following code:

try:
 settings.INSTALLED_APPS
except ImproperlyConfigured as exc:
 self.settings_exception = exc
except ImportError as exc:
 self.settings_exception = exc
if settings.configured:
 ....
else:
 django.setup() # Doesn't get called :)

The trick is to remove the exception handling (settings.INSTALLED_APPS). Copy the code from inside the try and place it above the try word, run python manage.py check and the real exception should be displayed instead of the "AppRegistryNotReady" obscure message. Now you can fix the real issue!

After fixing the issue, don't forget to revert the changes back!

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.