wagtail AbstractImage、ParentalManyToManyField 和 ClusterableModel
wagtail AbstractImage, ParentalManyToManyField and ClusterableModel
使用 wagtail 2.1、django 2.0.3、python 3.6.4
我有以下(简化的)自定义图像模型,通过 m2m 关系链接到 PhotoType
和 PhotoPlate
:
from wagtail.images.models import AbstractImage
from modelcluster.fields import ParentalManyToManyField
from modelcluster.models import ClusterableModel
class PhotoType(models.Model):
title = models.CharField(verbose_name='Title', max_length=255, blank=False, null=False, default=None)
class PhotoPlate(models.Model):
plate= models.CharField(verbose_name='Title', max_length=255, blank=False, null=False, default=None)
class Photo(AbstractImage):
type = ParentalManyToManyField(PhotoType, help_text="Several are allowed.", blank=True)
plate = ParentalManyToManyField(PhotoPlate, help_text="Several are allowed.", blank=True)
class Meta:
verbose_name = 'Photo'
verbose_name_plural = 'Photos'
PhotoType
和 PhotoPlate
模型通过本地 wagtail_hooks.py
文件中的 modeladmin_register(PhotoTypeModelAdmin)
和 modeladmin_register(PhotoPlateModelAdmin)
引用。
遵循 documentation.
后一切正常
除了一件事:无论在为 type
和 plate
这两个字段呈现的多选下拉列表中选择了多少项目,都不会保存相应的 m2m 关系。
我找到了一些 ,但可以通过处理照片 class 的继承来让它工作,例如:class CCAPhoto(ClusterableModel, AbstractImage)
.
有什么方法可以将 ParentalManyToManyField
添加到自定义图像模型中吗?如果是这样,我错过了什么?
编辑:
手动向数据库添加关系时,正确的项目会正确显示在 wagtail 管理表单上 - 即,在初始加载时预先选择。
与其使用 ParentalManyToManyField
,不如在此处使用普通的 ManyToManyField
:
class Photo(AbstractImage):
type = models.ManyToManyField(PhotoType, help_text="Several are allowed.", blank=True)
plate = models.ManyToManyField(PhotoPlate, help_text="Several are allowed.", blank=True)
ParentalManyToManyField
和 ParentalKey
字段类型设计用于 Wagtail 的页面编辑器(以及片段等相关区域),其中多个模型需要作为一个单元一起处理以进行预览和版本跟踪。 Wagtail 的图像和文档模型不使用它 - 它们由通过普通 Django ModelForm 编辑的单个模型组成,因此 ParentalManyToManyField
和 ParentalKey
不是必需的。
使用 wagtail 2.1、django 2.0.3、python 3.6.4
我有以下(简化的)自定义图像模型,通过 m2m 关系链接到 PhotoType
和 PhotoPlate
:
from wagtail.images.models import AbstractImage
from modelcluster.fields import ParentalManyToManyField
from modelcluster.models import ClusterableModel
class PhotoType(models.Model):
title = models.CharField(verbose_name='Title', max_length=255, blank=False, null=False, default=None)
class PhotoPlate(models.Model):
plate= models.CharField(verbose_name='Title', max_length=255, blank=False, null=False, default=None)
class Photo(AbstractImage):
type = ParentalManyToManyField(PhotoType, help_text="Several are allowed.", blank=True)
plate = ParentalManyToManyField(PhotoPlate, help_text="Several are allowed.", blank=True)
class Meta:
verbose_name = 'Photo'
verbose_name_plural = 'Photos'
PhotoType
和 PhotoPlate
模型通过本地 wagtail_hooks.py
文件中的 modeladmin_register(PhotoTypeModelAdmin)
和 modeladmin_register(PhotoPlateModelAdmin)
引用。
遵循 documentation.
后一切正常除了一件事:无论在为 type
和 plate
这两个字段呈现的多选下拉列表中选择了多少项目,都不会保存相应的 m2m 关系。
我找到了一些 class CCAPhoto(ClusterableModel, AbstractImage)
.
有什么方法可以将 ParentalManyToManyField
添加到自定义图像模型中吗?如果是这样,我错过了什么?
编辑: 手动向数据库添加关系时,正确的项目会正确显示在 wagtail 管理表单上 - 即,在初始加载时预先选择。
与其使用 ParentalManyToManyField
,不如在此处使用普通的 ManyToManyField
:
class Photo(AbstractImage):
type = models.ManyToManyField(PhotoType, help_text="Several are allowed.", blank=True)
plate = models.ManyToManyField(PhotoPlate, help_text="Several are allowed.", blank=True)
ParentalManyToManyField
和 ParentalKey
字段类型设计用于 Wagtail 的页面编辑器(以及片段等相关区域),其中多个模型需要作为一个单元一起处理以进行预览和版本跟踪。 Wagtail 的图像和文档模型不使用它 - 它们由通过普通 Django ModelForm 编辑的单个模型组成,因此 ParentalManyToManyField
和 ParentalKey
不是必需的。