wagtail 页面模型与自身的多对多关系?
many-to-many relationship of wagtail page model to itself?
所以我得到了一个带有 "companion" 字段的 PlantDetailPage 模型(是的,植物可以是伙伴),我应该可以在其中 select 其他 PlantDetailPages。我得到了要显示的东西,在线创建新植物(是的,菜单中的菜单中的菜单...),但问题很少:
1) 它不会 select 他们(点击 "select plantdetailpage" 没有任何动作)
2) "companions" 菜单按钮现在显示在左侧(就像它变成的一个片段?)- 我想避免这种情况。
3) 我不知道如何限制随行内联 select 或仅 selecting 而不是创建更多的 PlantDetailPages(这样就没有 windows 的递归) ?
这是 models.py 中的模型:
class PlantCompanion(ClusterableModel):
companion = models.ForeignKey(
"vegependium.PlantDetailPage", on_delete=models.SET_NULL, related_name="plants", null=True
)
plant = ParentalKey(
"vegependium.PlantDetailPage",
on_delete=models.SET_NULL,
related_name="companions",
null=True,
)
panels = [InstanceSelectorPanel("companion")]
class PlantDetailPage(Page):
genus_species = models.CharField(max_length=100, blank=False, null=False)
# soo many other fields
content_panels = Page.content_panels + [
#soo many other panels
FieldPanel("alternative_names")
],
heading=_("names")
),
MultiFieldPanel(heading=_("Companions"), children=[InlinePanel("companions")]),
#even more panels
]
def get_context(self, request):
context = super().get_context(request)
context["plant"] = self # needed?
# You always can access the page object via "page" or "self" in the template!
return context
并在 admin.py 中:
class CompanionAdmin(ModelAdmin):
"""Modeladmin definitions for sompanions."""
model = PlantDetailPage
menu_label = _("Companions")
menu_icon = "snippet"
menu_order = 499 # defines the menu position (e.g. after "images")
add_to_settings_menu = False
exclude_from_explorer = True
list_filter = [] # list attributes with only few choices
list_display = [
"genus_species",
] # columns to show up in admin (including one dynamic column)
search_fields = ["genus_species"] # columns to search in
modeladmin_register(CompanionAdmin)
from modelcluster.fields import ParentalManyToManyField
class PlantPage(Page):
related_plants = ParentalManyToManyField('self', blank=True)
content_panels = Page.content_panels + [
FieldPanel('related_plants'),
]
这种关系是对称的,如果A与B相关,则B与A相关。
文档https://docs.wagtail.io/en/stable/getting_started/tutorial.html?highlight=ParentalManyToManyField
该示例使用复选框小部件。
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
所以我得到了一个带有 "companion" 字段的 PlantDetailPage 模型(是的,植物可以是伙伴),我应该可以在其中 select 其他 PlantDetailPages。我得到了要显示的东西,在线创建新植物(是的,菜单中的菜单中的菜单...),但问题很少:
1) 它不会 select 他们(点击 "select plantdetailpage" 没有任何动作)
2) "companions" 菜单按钮现在显示在左侧(就像它变成的一个片段?)- 我想避免这种情况。
3) 我不知道如何限制随行内联 select 或仅 selecting 而不是创建更多的 PlantDetailPages(这样就没有 windows 的递归) ?
这是 models.py 中的模型:
class PlantCompanion(ClusterableModel):
companion = models.ForeignKey(
"vegependium.PlantDetailPage", on_delete=models.SET_NULL, related_name="plants", null=True
)
plant = ParentalKey(
"vegependium.PlantDetailPage",
on_delete=models.SET_NULL,
related_name="companions",
null=True,
)
panels = [InstanceSelectorPanel("companion")]
class PlantDetailPage(Page):
genus_species = models.CharField(max_length=100, blank=False, null=False)
# soo many other fields
content_panels = Page.content_panels + [
#soo many other panels
FieldPanel("alternative_names")
],
heading=_("names")
),
MultiFieldPanel(heading=_("Companions"), children=[InlinePanel("companions")]),
#even more panels
]
def get_context(self, request):
context = super().get_context(request)
context["plant"] = self # needed?
# You always can access the page object via "page" or "self" in the template!
return context
并在 admin.py 中:
class CompanionAdmin(ModelAdmin):
"""Modeladmin definitions for sompanions."""
model = PlantDetailPage
menu_label = _("Companions")
menu_icon = "snippet"
menu_order = 499 # defines the menu position (e.g. after "images")
add_to_settings_menu = False
exclude_from_explorer = True
list_filter = [] # list attributes with only few choices
list_display = [
"genus_species",
] # columns to show up in admin (including one dynamic column)
search_fields = ["genus_species"] # columns to search in
modeladmin_register(CompanionAdmin)
from modelcluster.fields import ParentalManyToManyField
class PlantPage(Page):
related_plants = ParentalManyToManyField('self', blank=True)
content_panels = Page.content_panels + [
FieldPanel('related_plants'),
]
这种关系是对称的,如果A与B相关,则B与A相关。
文档https://docs.wagtail.io/en/stable/getting_started/tutorial.html?highlight=ParentalManyToManyField 该示例使用复选框小部件。
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),