[英]metaclass conflict with modelformset_factory in Django
我正在使用Django模型繼承來創建兩個模型 - WorkAttachmentPicture
和WorkAttachmentAudio
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來創建表單:
class ImageAttachmentForm(forms.ModelForm):
""" Image attached to work """
image = forms.FileField(
label=_('File'),
help_text=_('JPEG, GIF or PNG image.')
)
description = forms.CharField(
widget=forms.Textarea(),
label=_('File description'),
help_text=_('Max. 500 symbols.'),
max_length=500
)
class Meta:
model = WorkAttachmentPicture
fields = ['image', 'description']
ImageAttachmentFormSet = modelformset_factory(WorkAttachmentPicture, form=ImageAttachmentForm)
class AudioAttachmentForm(forms.Form):
""" Audio file attached to work """
file = forms.FileField(
label=_('File'),
help_text=_('MP3 file.')
)
description = forms.CharField(
widget=forms.Textarea(),
label=_('File description'),
help_text=_('Max. 500 symbols.'),
max_length=500
)
class Meta:
model = WorkAttachmentAudio
fields = ['file', 'description']
AudioAttachmentFormSet = modelformset_factory(WorkAttachmentAudio, form=AudioAttachmentForm)
一切似乎對我來說都是正確的,但在項目啟動時我得到錯誤:
metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
如果我只創建一個formset(例如ImageAttachmentFormSet
),一切都可以。 但是當我添加另一個時,會出現錯誤。 如何解決這個問題,將模型集與繼承模型一起使用?
解決了它。 如果你仔細看
# this has forms.ModelForm
class ImageAttachmentForm(forms.ModelForm):
# this has forms.Form
class AudioAttachmentForm(forms.Form):
我已經改變了forms.Form
到forms.ModelForm
,現在一切正常 - 這是一個簡單的復制/粘貼錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.