我想使用
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
设置:
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")
我不久前为这个场景编写了一个自定义选择器,可能会有用。尚未完全完成,但它是一个工作模型。
我将代码发布在 Slack 上:https://wagtailcms.slack.com/archives/C81FGJR2S/p1693919153842339
它的工作原理是隐藏默认元素并通过弹出模式读取/写入该元素。