元类与Djang的modelformset_工厂冲突

2024-10-03 02:45:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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与继承的模型一起使用?在


Tags: ofto模型truethatmodelswidthclass
1条回答
网友
1楼 · 发布于 2024-10-03 02:45:06

解决了。如果你仔细看

# this has forms.ModelForm
class ImageAttachmentForm(forms.ModelForm):
# this has forms.Form
class AudioAttachmentForm(forms.Form):

我已经将forms.Form改为forms.ModelForm,现在一切都正常了——这是一个简单的复制/粘贴错误。在

相关问题 更多 >