ag抽象图像,ParentalManyToManyField和ClusterableModel

Hal*_*Hal 4 python django python-3.x wagtail

使用wagtail 2.1,django 2.0.3,python 3.6.4

我有以下的(简化)自定义图像模型,挂PhotoType,并PhotoPlate通过M2M关系:

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'
Run Code Online (Sandbox Code Playgroud)

PhotoTypePhotoPlate模式是通过引用modeladmin_register(PhotoTypeModelAdmin),并 modeladmin_register(PhotoPlateModelAdmin)在本地wagtail_hooks.py文件。

遵循文档后一切正常。

除了一件事:无论为两个字段type和呈现的多项选择下拉列表中选择了多少项,都plate永远不会保存对应的m2m关系。我找到了一些答案,但是可以通过玩Photo类的继承使其工作,例如:class CCAPhoto(ClusterableModel, AbstractImage)

有什么方法可以ParentalManyToManyField向自定义图像模型添加?如果是这样,我想念什么?

编辑:当手动添加关系到数据库时,正确的项目正确显示在wagtail管理表单上--即,在初始加载时预先选择。

gas*_*man 5

而不是使用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)
Run Code Online (Sandbox Code Playgroud)

ParentalManyToManyFieldParentalKey字段类型是专为鹡鸰的页面编辑器(以及相关的领域,如片段),其中多个模型需要处理一起作为一个单元进行预览和版本跟踪使用。鹡鸰的图像和文档模型不利用这一点-它们由通过普通的Django的ModelForm编辑的单一模式,所以ParentalManyToManyFieldParentalKey没有必要。