如何使用Django模型继承与信号? [英] How to use Django model inheritance with signals?
问题描述
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屋!