many-to-many relationship of wagtail page model to itself?

巧了我就是萌 提交于 2021-02-10 14:47:44

问题


So i got a PlantDetailPage model with "companion" field among others (yes plants can be companions), in which I should be able to select other PlantDetailPages. I got the thing to show up, create new plants in inline (yes, a menu in the menu in the menu...), but there's few issues:

1) It just won't select them (no action on clicking "select plantdetailpage")

2) "companions" menu button is now shown on the left (like a snippet that it became?) - which I'd like to avoid.

3) I don't know how to limit the companion inline selector to only selecting and not creating more PlantDetailPages (so that there's no recursion of windows) ?

Here's the model in 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

and in 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)

回答1:


from modelcluster.fields import ParentalManyToManyField

class PlantPage(Page):
    related_plants = ParentalManyToManyField('self', blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('related_plants'),
    ]

This relation is symmetric, if A is related to B, B is related to A.

Docs https://docs.wagtail.io/en/stable/getting_started/tutorial.html?highlight=ParentalManyToManyField The example uses a checkbox widget.

FieldPanel('categories', widget=forms.CheckboxSelectMultiple),


来源:https://stackoverflow.com/questions/62399603/many-to-many-relationship-of-wagtail-page-model-to-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!