r4r3devAut
r4r3devAut

Reputation: 90

Wagtail Custom Inline PageChooser

Currently i face a strange problem, using wagtail. I modified a Snippet from the Documentation. But it seems like i miss something. This is my Code...

The Goal is to allow selecting multible Pages using PageChooser (and maybe later show links in template)

class BlogPage(Page):
    content_panels = Page.content_panels + [
        MultiFieldPanel([
            InlinePanel('related_pages', label="Related Pages"),
        ])
]

class BlogPageRelated(Orderable):
    page = ParentalKey('home.BlogPage', on_delete=models.CASCADE, related_name='related_pages')
    relpages = models.ForeignKey(
        'wagtailcore.Page', on_delete=models.CASCADE, related_name='+', blank=True, null=True
    )
    panels = [
    PageChooserPanel('relpages', 'home.BlogPage'),
]

The Database is filled with data. It seems like no data are delivered to the template. The template variable {{ page.related_pages }} outputs "home.BlogPageRelated.None".

{{ page.related_pages }} = home.BlogPageRelated.None 

For better understanding this is the snipped i adopted - (!) and it works well!

class BlogPageImage(Orderable):
    page = ParentalKey(BlogPage, on_delete=models.CASCADE, related_name='gallery_images')
    image = models.ForeignKey(
        'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
    )

    panels = [
        ImageChooserPanel('image'),
    ]

Upvotes: 1

Views: 1072

Answers (1)

r4r3devAut
r4r3devAut

Reputation: 90

Finally i got the solution by myself. This inline elements are kind of strange and imho a bit intransparent. Nvm, Wagtail is the trouble worth.

My Inline-Model :

class RelatedPage(models.Model):
    page = ParentalKey(BlogPage, on_delete=models.CASCADE, related_name='relpages', default=None)
    relpage = models.ForeignKey(
        'wagtailcore.Page', on_delete=models.CASCADE, related_name='+'
    )
    panels = [
        PageChooserPanel('relpage', 'home.BlogPage'),
    ]

My Template:

{% for x in page.relpages.all %}
    <a href="{% pageurl x.relpage %}">ax {{ x.specific.title }}</a>
{% endfor %}

I missed the "all" in "...page.relpages.all ..."

Upvotes: 1

Related Questions