Search code examples
pythonanywhere

django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. tastypie


Continuing my search for an answer to get oauth2.0 to work on pythonanywhere.

i am following this tutorial: http://ianalexandr.com/blog/building-a-true-oauth-20-api-with-django-and-tasty-pie.html

im using django 1.6 : https://www.pythonanywhere.com/wiki/VirtualEnvForNewerDjango

when i get to this line of codes:

   from provider.oauth2.models import Client
   # from django.contrib.auth.models import User
   from django.contrib.auth import get_user_model

   User = get_user_model()
   u = User.objects.get(id=1)
   c = Client(user=u, name="mysite client", client_type=1, url="http://pythonx00x.pythonanywhere.com")
   c.save()
   c.client_id
   'd63f53a7a6cceba04db5'
   c.client_secret
   'afe899288b9ac4127d57f2f12ac5a49d839364dc'

it seems that i got an error at line:

   User = get_user_model()

and it raise an error:

   raise AppRegistryNotReady("Models aren't loaded yet.")
   django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

here is the full stack trace:

   Traceback (most recent call last):
   File "addClient.py", line 9, in <module>
   User = get_user_model()
   File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 136, in get_user_model
   return django_apps.get_model(settings.AUTH_USER_MODEL)
   File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/apps/registry.py", line 200, in get_model
   self.check_models_ready()
   File "/home/python2006/.virtualenvs/django16/local/lib/python2.7/site-packages/django/apps/registry.py", line 132, in check_models_ready
   raise AppRegistryNotReady("Models aren't loaded yet.")
   django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

I can't seem to find out how to get the models load. if I'm getting the idea right.


Solution

  • I think you may not be using the version of Django that you think you are. AppRegistryNotReady was introduced in Django 1.7. I would guess that, if you pinned your DJango version to 1.6, your code would work.