如何使用Django模型继承与信号? [英] How to use Django model inheritance with signals?

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

问题描述

我在Django中有几个模型继承级别:

  class WorkAttachment(models.Model):
包含每个附件中所需的所有字段的抽象类
work = models.ForeignKey(Work)
added = models.DateTimeField(default = datetime.datetime.now)
views = models.IntegerField(default = 0)

class Meta:
abstract = True


class WorkAttachmentFileBased(WorkAttachment):
另一个基类,但是对于基于文件的附件
description = models.CharField(max_length = 500,blank = True)
size = models.IntegerField(verbose_name = _以字节为单位))

class Meta:
abstract = True


class WorkAttachmentPicture(WorkAttachmentFileBased):
附加图片工作
image = models.ImageField(upload_to ='works / images',width_field ='wid th',height_field ='height')
width = models.IntegerField()
height = models.IntegerField()

WorkAttachmentFileBased WorkAttachment 继承了许多不同的模型。当创建附件时,我想创建一个信号,该信号将更新一个 attachment_count 字段用于父工作。认为为父发送者( WorkAttachment )发出的信号也会对所有继承的模型运行,但不会。这是我的代码:

  @receiver(post_save,sender = WorkAttachment,dispatch_uid =att_post_save)
def update_attachment_count_on_save (发件人,实例,** kwargs):
更新保存附件时工作的文件数。
instance.work.attachment_count + = 1
instance.work。 save()

有没有办法使这个信号适用于从<$ c $继承的所有模型c> WorkAttachment ?



Python 2.7,Django 1.4 pre-alpha



PS我已经尝试过我在网上找到的一个解决方案 ,但对我来说并不奏效。

解决方案

你可以尝试一下:

  model_classes = [WorkAttachment,WorkAttachmentFileBased,WorkAttachmentPicture,...] 

def update_attachment_count_on_save(发件人,实例,** kwargs):
instance.work.attachment_count + = 1
instance.work.save()

for model_classes中的model_class:
post_save.connect(update_attachment_count_on_save,
sender = model_class ,
dispatch_uid =att_post_save _+ model_class .__ name__)

(免责声明:我没有测试了以上)


I have a few model inheritance levels in Django:

class WorkAttachment(models.Model):
    """ Abstract class that holds all fields that are required in each attachment """
    work            = models.ForeignKey(Work)
    added           = models.DateTimeField(default=datetime.datetime.now)
    views           = models.IntegerField(default=0)

    class Meta:
        abstract = True


class WorkAttachmentFileBased(WorkAttachment):
    """ Another base class, but for file based attachments """
    description     = models.CharField(max_length=500, blank=True)
    size            = models.IntegerField(verbose_name=_('size in bytes'))

    class Meta:
        abstract = True


class WorkAttachmentPicture(WorkAttachmentFileBased):
    """ Picture attached to work """
    image           = models.ImageField(upload_to='works/images', width_field='width', height_field='height')
    width           = models.IntegerField()
    height          = models.IntegerField()

There are many different models inherited from WorkAttachmentFileBased and WorkAttachment. I want to create a signal, which would update an attachment_count field for parent work, when attachment is created. It would be logical, to think that signal made for parent sender (WorkAttachment) would run for all inherited models too, but it does not. Here is my code:

@receiver(post_save, sender=WorkAttachment, dispatch_uid="att_post_save")
def update_attachment_count_on_save(sender, instance, **kwargs):
    """ Update file count for work when attachment was saved."""
    instance.work.attachment_count += 1
    instance.work.save()

Is there a way to make this signal work for all models inherited from WorkAttachment?

Python 2.7, Django 1.4 pre-alpha

P.S. I've tried one of the solutions I found on the net, but it did not work for me.

解决方案

You could try something like:

model_classes = [WorkAttachment, WorkAttachmentFileBased, WorkAttachmentPicture, ...]

def update_attachment_count_on_save(sender, instance, **kwargs):
    instance.work.attachment_count += 1
    instance.work.save()

for model_class in model_classes:
    post_save.connect(update_attachment_count_on_save, 
                      sender=model_class, 
                      dispatch_uid="att_post_save_"+model_class.__name__)

(Disclaimer: I have not tested the above)

这篇关于如何使用Django模型继承与信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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