Use signals in Django 1.9

In Django 1.8, I was able to do the following with my signals, and everything was fine:

__ __ INIT ru :.

from .signals import * 

signals.py:

 @receiver(pre_save, sender=Comment) def process_hashtags(sender, instance, **kwargs): html = [] for word in instance.hashtag_field.value_to_string(instance).split(): if word.startswith('#'): word = render_to_string('hashtags/_link.html', {'hashtag': word.lower()[1:]}) html.append(word) instance.hashtag_enabled_text = ' '.join(html) 

In Django 1.9, I get this error: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I know this comes from __init__.py , but does anyone know about this? I guess maybe put it in a model? If yes, can someone please show me how to do this?

models.py:

 class Comment(HashtagMixin, TimeStampedModel): user = models.ForeignKey(settings.AUTH_USER_MODEL) text = models.TextField(max_length=240) hashtag_enabled_text = models.TextField(blank=True) hashtag_text_field = 'text' objects = CommentManager() class Meta: app_label = 'comments' def __unicode__(self): return self.text 

Thank you in advance!

+3
python django django-models django-signals
Dec 15 '15 at 23:00
source share
1 answer

From the release note :

All models must be defined inside the installed application or declare an explicit app_label label. In addition, it is impossible to import them before downloading the application. In particular, it is not possible to import models inside the application root package.

By importing your signals into __init__.py , you indirectly import your models into the root package of your application. One option to avoid this is to change sender to a line:

 @receiver(pre_save, sender='<appname>.Comment') def process_hashtags(sender, instance, **kwargs): ... 

The recommended way to connect signals that use @receiver decorator in 1.9 is to configure the application and import the signal module into AppConfig.ready() .

+4
Dec 15 '15 at 23:10
source share



All Articles