我在 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()
WorkAttachmentFileBased
从和继承了许多不同的模型WorkAttachment
。我想创建一个信号,它会attachment_count
在创建附件时更新父工作的字段。认为为父发送者 ( WorkAttachment
) 发出的信号也适用于所有继承的模型是合乎逻辑的,但事实并非如此。这是我的代码:
@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()
有没有办法让这个信号适用于所有继承自的模型WorkAttachment
?
Python 2.7、Django 1.4 pre-alpha
PS 我已经尝试了我在网上找到的一种解决方案,但它对我不起作用。