Wagtail 后端:如何为 `ParentalManyToManyField` 使用 `MultipleChooserPanel`

问题描述 投票:0回答:2

我想使用

MultipleChooserPanel
[1] 作为
ParentalManyToManyField
[2]:

class Article(Page):
    authors = ParentalManyToManyField(
        'profile.ProfilePage',
        blank=True,
        limit_choices_to={'is_author': True},
        related_name='articles',
    )

    content_panels = Page.content_panels + [
        MultipleChooserPanel(
            '????',  # <- relation_name, see [1]
            chooser_field_name="???",
            label="Authors",
        )
    ]

但说实话,我不知道在这里输入什么

relation_name
chooser_field_name


[1] https://docs.wagtail.org/en/stable/reference/pages/panels.html#multiplechooserpanel
[2] https://docs.wagtail.org/en/v5.2/getting_started/tutorial.html#codecell33
[2] https://github.com/wagtail/django-modelcluster/blob/main/modelcluster/fields.py#L506


设置:

  • Django 4.2.7
  • 鹡鸰5.2
wagtail
2个回答
0
投票

MultipleChooserPanel
目前仅支持一对多关系。您需要将“作者”关系转换为链接到 ProfilePage 的子模型:

class ArticleAuthor(Orderable):
    page = ParentalKey(Article, on_delete=models.CASCADE, related_name='authors')
    profile_page = models.ForeignKey(
        'profile.ProfilePage', on_delete=models.CASCADE, related_name='+'
    )

    panels = [
        FieldPanel('profile_page'),
    ]

然后使用:

MultipleChooserPanel('authors', label="Authors", chooser_field_name="profile_page")


0
投票

我不久前为这个场景编写了一个自定义选择器,可能会有用。尚未完全完成,但它是一个工作模型。

我将代码发布在 Slack 上:https://wagtailcms.slack.com/archives/C81FGJR2S/p1693919153842339

它的工作原理是隐藏默认元素并通过弹出模式读取/写入该元素。

© www.soinside.com 2019 - 2024. All rights reserved.