我使用Django模型继承来创建两个模型WorkAttachmentPicture
和{
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()
class WorkAttachmentAudio(WorkAttachmentFileBased):
""" Audio file attached to work """
file = models.FileField(upload_to='works/audio')
一个作品可以有多个音频和视频附件,所以我使用modelformset_factory来创建表单:
^{pr2}$在我看来一切都是正确的,但在项目启动时,我得到了一个错误:
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
如果我只创建一个formset(ImageAttachmentFormSet
),那么一切都正常。但是当我添加另一个时,错误出现了。如何解决这个问题,将modelformsets与继承的模型一起使用?在
解决了。如果你仔细看
我已经将
forms.Form
改为forms.ModelForm
,现在一切都正常了——这是一个简单的复制/粘贴错误。在相关问题 更多 >
编程相关推荐