AppRegistryNotReady: Models aren't loaded yet.

1,262 views
Skip to first unread message

Sabine Maennel

unread,
Sep 24, 2014, 8:12:41 AM9/24/14
to django...@googlegroups.com
Hello, please help!

Some of my models throw this error, when I query them with object.all(). They coexist in apps that have other models that are just fine. I do not understand at all what the problem might be and could not google it. 

For example: this would be my models.py in the app "account":

from django.db import models
from django.contrib.auth.models import User
from apps.courseevent.models import CourseParticipation
from apps.transaction.models import Transaction
from model_utils.fields import SplitField
from model_utils.models import TimeStampedModel
from money import Money

class UserStatus(TimeStampedModel):
    status_code = models.CharField(max_length=20)
    title = models.CharField(max_length=100)
    text = models.TextField()

    def __unicode__(self):
        return u"%s" % (self.title)

class UserAccount(TimeStampedModel):
    user = models.ForeignKey(User, unique=True)
    about = SplitField()
    status = models.ForeignKey(UserStatus)

    def __unicode__(self):
        return u"%s %s" % (self.user.first_name, self.user.last_name)

    def balance(self):
        balance = Money(amount='0.00', currency='EUR')
        transactions = Transaction.objects.filter(user=self.user_id)
        for transaction in transactions:
            try:
               transaction_value = Money(amount=transaction.total , currency='EUR' )
               balance = balance + transaction_value
            except:
               pass
        return balance

    def participation_level(self):
        try:
            CourseParticipation.objects.filter(participant=self.user_id,
                role = CourseParticipation.ROLE_TEACHER)
            return CourseParticipation.ROLE_TEACHER
        except:
            try:
                CourseParticipation.objects.filter(participant=self.user_id,
                    role = CourseParticipation.ROLE_STUDENT)
                return CourseParticipation.ROLE_STUDENT
            except:
                return None


Then I try to call them via the Python.shell:
>>>>from apps.account.models import UserStatus, UserAccount 
>>>>x =  UserStatus.objects.all()
>>>>y = UserAccount.objects.all()
>>>>print x
[<UserStatus: Angemeldet>, <UserStatus: Lehrer-Anwaerter>]
>>>>print y
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/query.py", line 119, in __repr__
    return repr(data)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/base.py", line 459, in __repr__
    u = six.text_type(self)
  File "/Users/sabinemaennel/PycharmProjects/netteachers/apps/account/models.py", line 23, in __unicode__
    return u"%s %s" % (self.user.first_name, self.user.last_name)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py", line 557, in __get__
    val = self.field.get_local_related_value(instance)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1429, in get_local_related_value
    return self.get_instance_value_for_fields(instance, self.local_related_fields)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1422, in local_related_fields
    return tuple(lhs_field for lhs_field, rhs_field in self.related_fields)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1413, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1406, in resolve_related_fields
    else self.rel.to._meta.get_field_by_name(to_field_name)[0])
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/options.py", line 416, in get_field_by_name
    cache = self.init_name_map()
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/options.py", line 445, in init_name_map
    for f, model in self.get_all_related_m2m_objects_with_model():
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/options.py", line 563, in get_all_related_m2m_objects_with_model
    cache = self._fill_related_many_to_many_cache()
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/options.py", line 577, in _fill_related_many_to_many_cache
    for klass in self.apps.get_models():
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/apps/registry.py", line 168, in get_models
    self.check_models_ready()
  File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
AppRegistryNotReady: Models aren't loaded yet.

Tom Evans

unread,
Sep 24, 2014, 9:05:35 AM9/24/14
to django...@googlegroups.com
On Wed, Sep 24, 2014 at 1:12 PM, Sabine Maennel
<sabine....@gmail.com> wrote:
> Hello, please help!
>
> Some of my models throw this error, when I query them with object.all().
> They coexist in apps that have other models that are just fine. I do not
> understand at all what the problem might be and could not google it.

The failure is when django attempts to generate the unicode
representation of that model in order to fill in references to related
objects on those instances.

File "/Users/sabinemaennel/PycharmProjects/netteachers/apps/account/models.py",
line 23, in __unicode__
return u"%s %s" % (self.user.first_name, self.user.last_name)
File "/Users/sabinemaennel/VirtualEnvs/netteachers/lib/python2.7/site-packages/django/db/models/fields/related.py",
line 557, in __get__
val = self.field.get_local_related_value(instance)

Specifically, it happens when you attempt to access the
UserAccount.user atttribute, which references the User model.

At this point, django complains that this app is not ready and the
models aren't loaded. Do you have 'django.contrib.auth' in
settings.INSTALLED_APPS?

Cheers

Tom

Sabine Maennel

unread,
Sep 24, 2014, 10:15:08 AM9/24/14
to django...@googlegroups.com
Thank you Tom,

that was very helpful. It all works if I throw these unicode representations out. I do have  'django.contrib.auth' installed. 
I can play around with what representations to use now. But from the error message I would not have guessed that it had this reason. So again thanks very much, you saved my day!

        with kind regards
            Sabine

Collin Anderson

unread,
Sep 24, 2014, 10:38:02 AM9/24/14
to django...@googlegroups.com
Are you using a plain python shell or ./manage.py shell? If you are using a plain python shell, you must call django.setup() first.

Sabine Maennel

unread,
Sep 24, 2014, 10:46:05 AM9/24/14
to django...@googlegroups.com
I am using Pycharm and I did declare my project as a Django project. So how can call django.setup()? Do I do that in my settings-file?


On Wed, Sep 24, 2014 at 4:38 PM, Collin Anderson <cmawe...@gmail.com> wrote:
Are you using a plain python shell or ./manage.py shell? If you are using a plain python shell, you must call django.setup() first.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/r4BaiFdqMeY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/809ad79d-0036-4ddd-ac1c-a417ed25eed3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Sabine Maennel
Leimbachstr. 223
8041 Zürich
Tel. +41 (0) 43 53 778 74
Mathematikerin / Softwareingenieurin

Russell Keith-Magee

unread,
Sep 24, 2014, 7:53:15 PM9/24/14
to Django Users
Hi Sabine,

No - you don't put it in your settings file; the settings file is loaded as part of the setup call.

As a completely manual process, you can just run the following:

import django
django.setup()

as the first two commands in your Python shell. If PyCharm has any hooks you can tie into for providing shell startup logic, that would be a good place to put these two lines. 

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Sabine Maennel

unread,
Sep 25, 2014, 3:12:02 AM9/25/14
to django...@googlegroups.com
Thanks Rusell, that helps.


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages