为什么 Django 模型信号不起作用? [英] Why Django model signals are not working?

查看:38
本文介绍了为什么 Django 模型信号不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的状态创建用户的活动流.

I am trying to create activity streams of users from their status.

型号:

class Status(models.Model):
    body = models.TextField(max_length=200)
    image = models.ImageField(blank=True, null=True, upload_to=get_upload_file_name)
    privacy = models.CharField(max_length=1,choices=PRIVACY, default='F')
    pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)
    user = models.ForeignKey(User)

class Activity(models.Model):
    actor = models.ForeignKey(User)
    action = models.CharField(max_length=100)
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    pub_date = models.DateTimeField(auto_now_add=True, auto_now=False)

然而,虽然我创建了一个新状态,但它并没有从 post_save 信号创建一个新活动.

However, although I create a new status, it does not create a new activity from the post_save signal.

信号:

from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import post_save
from status.models import Status
from models import Activity

def create_activity_item(sender, instance, signal, *args, **kwargs):
    if kwargs.get('created', True):
        ctype = ContentType.objects.get_for_model(instance)

        if ctype.name == 'Status':
            action = ' shared '

            activity = Activity.objects.get_or_create(
                actor = instance.user,
                action = action,
                content_type = ctype,
                object_id = instance.id,
                pub_date = instance.pubdate
            )

post_save.connect(create_activity_item, sender=Status)

我做错了什么?请帮我解决这个问题.我将不胜感激.谢谢.

What am I doing wrong? Please help me solve this problem. I will be very much grateful. Thank you.

更新:

但是这样做会创建活动:

However doing like this creates the activity:

@receiver(post_save, sender=Status)
def create(sender, instance, **kwargs):
    if kwargs.get('created',True):
        ctype = ContentType.objects.get_for_model(instance)
        activity = Activity.objects.get_or_create(
            actor = instance.user,
            action = ' shared ',
            content_type = ctype,
            object_id = instance.id,
            pub_date = instance.pub_date
        )

为什么上述方法不起作用?

Why doesn't the above works then?

推荐答案

好像你的 post_save.connect 没有执行.您应该在某处导入 signals.对于 django 1.7,建议在应用程序的 config ready() 函数中执行此操作.阅读这段代码应该放在哪里?"文档中的旁注.

Seems like your post_save.connect is not executed. You should import signals somewhere. For django 1.7 it is recommended to do this in the app's config ready() function. Read the "Where should this code live?" side note in the docs.

例如,如果您的应用名为 activity:

For example if your app is called activity:

activity/__init__.py

default_app_config = 'activity.apps.ActivityAppConfig'

activity/apps.py

from django.apps import AppConfig

class ActivityAppConfig(AppConfig):
    name = 'activity'

    def ready(self):
        import activity.signals

并且不要忘记添加dispatch_uid 到您的 connect() 调用:

And don't forget to add dispatch_uid to your connect() call:

post_save.connect(create_activity_item, sender=Status,
                  dispatch_uid="create_activity_item")

UPDATE:ContentTypename 属性总是小写.所以你应该把 if 语句改为:

UPDATE: name attribute of ContentType is always in lower case. So you should change the if statement to:

if ctype.name == 'status':

这篇关于为什么 Django 模型信号不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆