Apps aren't loaded yet. with signals

Bussiere picture Bussiere · Oct 16, 2015 · Viewed 8k times · Source

I have an app not ready style error when i use signal. I think this is due to the User auth in the profile model , from what i've see using google there is some issue with the user auth.

i think that the error is here :

class Profile_User(models.Model):
    user = models.OneToOneField(User, null=True)

but i have no clue on how to solve it.

The model with the signal :

from django.db import models 
from Modif_Box.models import Modif_Box_User
from Profile.models import Profile_User
from Post.models import Post_User
from django.utils import timezone
#from Modif_Box.get_models import get_modif_box_profile




# Create your models here.
class Box_Post_User(models.Model):
    receiver =  models.OneToOneField("Profile.Profile_User",related_name="receiver_post_box",blank=True,null=True)
    post = models.ManyToManyField("Post.Post_User",blank=True,related_name="post_in_box_user")
    modif_box = models.ForeignKey("Modif_Box.Modif_Box_User",blank=True,null=True)
    last_viewed = models.IntegerField(blank=True,null=True)
    date = models.DateTimeField(default=timezone.now, blank=True)
    date_sys_added = models.DateTimeField(default=timezone.now, blank=True)
    def __str__(self)  :
        try :
            return "%s : %s : %s" % (self.receiver.number,self.post,self.post_personna)
        except :
            return "%s : %s : %s" % (self.receiver,self.post,self.post_personna)
    def __unicode__(self):
        return "%s : %s : %s " % (self.receiver.number,self.post,self.post_personna)

class Box_Post_Personna_User(models.Model):
    receiver =  models.OneToOneField("Profile.Profile_User",related_name="receiver_post_box_personna",blank=True,null=True)
    post_personna = models.ManyToManyField("Post.Post_Personna",blank=True,related_name="post_in_box_personna_user")
    modif_box = models.ForeignKey("Modif_Box.Modif_Box_User",blank=True,null=True)
    last_viewed = models.IntegerField(blank=True,null=True)
    date = models.DateTimeField(default=timezone.now, blank=True)
    date_sys_added = models.DateTimeField(default=timezone.now, blank=True)
    def __str__(self)  :
        try :
            return "%s : %s : %s" % (self.receiver.number,self.post,self.post_personna)
        except :
            return "%s : %s : %s" % (self.receiver,self.post,self.post_personna)
    def __unicode__(self):
        return "%s : %s : %s " % (self.receiver.number,self.post,self.post_personna)



from django.db.models.signals import m2m_changed
#from Post_Box.models import Box_Post_User
#from Post.models import Post_User
from django.dispatch import receiver


@receiver(m2m_changed, sender=Box_Post_User.post)
def box_post_user_changed(sender, **kwargs):
    print("signal")
    print("m2m")
    if kwargs['action'] == "post_add" and kwargs["model"] == Post_User :
        obj=kwargs['instance']
        mb = obj.modif_box
        pk_set = kwargs.get('pk_set', []) or []
        for pk in pk_set :
            mb.new_post_user.add(pk)

The profile model :

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from Profile.profile_script import Token_Generator
from django.contrib.auth.models import User
# Create your models here.
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 


class Profile_User(models.Model):
    user = models.OneToOneField(User, null=True)
    name = models.TextField(blank=True,null=True)
    token = models.TextField(blank=True,null=True)
    token_google = models.TextField(blank=True,null=True)
    langue = models.TextField(blank=True)
    country = models.TextField(blank=True)
    avatar_base64 = models.TextField(blank=True)
    number = models.TextField(blank=True)
    active = models.BooleanField(default=True)
    date_sys_added = models.DateTimeField(default=timezone.now, blank=True)
    nuyen = models.IntegerField(blank=True,null=True)
    date = models.DateTimeField(default=timezone.now, blank=True)
    mu = models.FloatField(null=True,blank=True)
    sigma = models.FloatField(null=True,blank=True)
    def __str__(self)  :
        return "%s : %s" % (self.name,self.number)
    def __unicode__(self):
        return "%s" % (self.user)
    def save(self, *args, **kwargs):
        #print("token")
        #print(self.token)
        if self.token is None or self.token == "" :
            #print("titi")
            super(Profile_User, self).save(*args, **kwargs)
            self.token = Token_Generator(self.id)
        super(Profile_User, self).save(*args, **kwargs)



class Type_Personna(models.Model):
    name = models.CharField(max_length=20,blank=True,null=True)
    date_sys_added = models.DateTimeField(default=timezone.now, blank=True)
    date = models.DateTimeField(default=timezone.now, blank=True)

class Personna(Profile_User):
    type_personna = models.ForeignKey("Type_Personna")
    def __unicode__(self):
        return "%s" % (self.name)

My error :

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 177, in fetch_command
    commands = get_commands()
  File "/usr/lib/python3.4/functools.py", line 448, in wrapper
    result = user_function(*args, **kwds)
  File "/usr/local/lib/python3.4/dist-packages/django/core/management/__init__.py", line 72, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

Regards and thanks

Answer

Eduardo Bas&#237;lio picture Eduardo Basílio · Dec 2, 2015

In django 1.9

# it is not need edit __init__.py

apps.py

from django.apps import AppConfig

class YourAppConfig(AppConfig):

    name = 'your_app_name'
    verbose_name = 'Your App Name' # optional

    def ready(self):
        from your.app.path import signals # import your signals.py

settings.py

# include path to YourAppConfig class:

INSTALLED_APPS = [
    'your_app.apps.YourAppConfig',
    # ...,
]

see django docs