应用程序尚未加载。有信号

Apps aren't loaded yet. with signals

我在使用信号时遇到应用未就绪样式错误。我认为这是由于配置文件模型中的用户身份验证所致,从我使用 google 看到的情况来看,用户身份验证存在一些问题。

我认为错误在这里:

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.

问候和感谢

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

在你的__init.py__文件中:

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

对涉及注册信号的每个应用执行此操作。

Read this 了解更多信息。

有几种方法可以尝试:

  • 删除 "from django.contrib.auth import get_user_model" 无论如何你都不使用它并且在加载应用程序之前它不起作用。我不知道单独导入是否会触发问题,或者您是否没有使用它,但安全总比抱歉好。
  • 如果你有模型包(带有 init.py 的目录)而不是 models.py 文件,那么你需要显式声明 app_label在他们 "class Meta".
  • 中使用的每个模型中
  • 如果 none 该工作正常,请尝试在应用程序配置就绪方法中移动您认为导致该问题的代码。

例如:

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 => ProfileUser。

祝你好运!

在 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