Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apps aren't loaded yet. with signals

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

like image 532
Bussiere Avatar asked Oct 16 '15 07:10

Bussiere


3 Answers

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

like image 143
Eduardo Basílio Avatar answered Nov 18 '22 21:11

Eduardo Basílio


Try registering signals when the app first loads.

In you __init.py__ file:

default_app_config = 'yourappname.apps.YourAppConfig'

In apps.py of the app:

from django.apps import AppConfig


class YourAppConfig(AppConfig):
    name = 'yourappname'

    def ready(self):
        from yourappname import signals

Do this for every app involved in registering signals.

Read this for more info.

like image 17
v1k45 Avatar answered Nov 18 '22 20:11

v1k45


There are several things to try:

  • remove "from django.contrib.auth import get_user_model" has you don't use it anyway and it doesn't work before the app is loaded. I don't know if importing alone triggers the issue or if you have ot use it, but better safe than sorry.
  • if you have a models packages (a dir with init.py) instead of a models.py file, then you need to declare explicitly app_label in every model you use in their "class Meta".
  • if none of that work, try to move the code you think causes that in the application config ready method.

E.G:

from django.apps import AppConfig


class ProductsConfig(AppConfig):
    name = 'your_app_name'

    def ready(self):
        YourModel = self.get_model('YourModel')
        # do stuff with the model

BTW: Models, as all classes, are better named without underscores such as Profile_User => ProfileUser.

Good luck !

like image 3
john doe Avatar answered Nov 18 '22 21:11

john doe