应用尚未加载。有信号

时间:2015-10-16 07:50:45

标签: django python-3.x django-signals

当我使用信号时,我有一个应用程序没有准备好样式错误。我认为这是由于个人资料模型中的用户身份验证,我使用谷歌看到的用户身份验证存在一些问题。

我认为错误就在这里:

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

但我不知道如何解决它。

带有信号的模型:

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)

个人资料模型:

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)

我的错误:

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.

问候并感谢

3 个答案:

答案 0 :(得分:16)

尝试在应用首次加载时注册信号。

在你default_app_config = 'yourappname.apps.YourAppConfig' 档案中:

apps.py

在应用的from django.apps import AppConfig class YourAppConfig(AppConfig): name = 'yourappname' def ready(self): from yourappname import signals 中:

{{1}}

为注册信号所涉及的每个应用程序执行此操作。

Read this了解更多信息。

答案 1 :(得分:16)

在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

答案 2 :(得分:2)

有几件事要尝试:

  • 删除“从django.contrib.auth导入get_user_model”你有没有使用它,它在加载应用程序之前不起作用。我不知道单独导入是否会触发问题,或者您是否使用它,但是比抱歉更安全。
  • 如果你有一个模型包(带有 init .py的目录)而不是models.py文件,那么你需要在你的“类Meta”中使用的每个模型中明确声明app_label
  • 如果没有这个工作,请尝试移动您认为导致应用程序配置就绪方法的代码。

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

顺便说一句:模型,作为所有类,更好地命名没有下划线,如Profile_User =&gt; ProfileUser。

祝你好运!