ParentalManyToManyField

ParentalManyToManyField

本文介绍了ag抽象图像,ParentalManyToManyField和ClusterableModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用wagtail 2.1,django 2.0.3,python 3.6.4

Using wagtail 2.1, django 2.0.3, python 3.6.4

我有以下(简化的)自定义图像模型,它们通过m2m关系链接到 PhotoType PhotoPlate :

I have the following (simplified) custom image model, linked to PhotoType and PhotoPlate via m2m relationships:

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 模型是通过 modeladmin_register(PhotoTypeModelAdmin) modeladmin_register(PhotoPlateModelAdmin)引用的本地 wagtail_hooks.py 文件.

The PhotoType and PhotoPlate models are referenced via modeladmin_register(PhotoTypeModelAdmin) and modeladmin_register(PhotoPlateModelAdmin) in the local wagtail_hooks.py file.

在遵循文档后,一切正常

除了一件事:为两个字段 type plate 呈现的多选下拉列表中选择了多少项,对应的m2m关系为从来没有保存过.我发现了一些答案,但是可以通过使用Photo类的继承,例如: class CCAPhoto(ClusterableModel,AbstractImage).

Except for one thing: no matter how many items are selected in the multiple choice dropdowns that are rendered for the two fields type and plate, the corresponding m2m relationship is never saved.I found a few answers, but could get it to work by playing with the inheritance of the Photo class, for example: class CCAPhoto(ClusterableModel, AbstractImage).

是否可以将 ParentalManyToManyField 添加到自定义图像模型中?如果是这样,我想念什么?

Is there any way to add a ParentalManyToManyField to a custom image model? If so, what am I missing?

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

When manually adding a relationship to the database, the right items are properly displayed on the wagtail admin form - -i.e., pre-selected on initial load.

推荐答案

而不是使用 ParentalManyToManyField ,您应该在此处使用简单的 ManyToManyField :

Rather than using ParentalManyToManyField, you should use a plain ManyToManyField here:

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 .

The ParentalManyToManyField and ParentalKey field types are designed for use in Wagtail's page editor (and related areas such as snippets), where multiple models need to be treated together as a single unit for previewing and version tracking. Wagtail's image and document models don't make use of this - they consist of a single model edited through an ordinary Django ModelForm, so ParentalManyToManyField and ParentalKey aren't necessary.

这篇关于ag抽象图像,ParentalManyToManyField和ClusterableModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 12:56