Wagtail: Can snippets have InlinePanel if the model has a ForeignKey relationship?
Asked Answered
Z

1

5

I've got the situation where a Wagtail snippet is a model that has a FK relationship. I can't figure out how to make that available in the CMS as an inline.

Given:

@register_snippet
class TeamMember(models.Model):
    name = models.CharField(max_length=80)
    (other fields)

    content_panels = [
        FieldPanel('name'),
        (etc.)
        #InlinePanel('tasks', label="Team Tasks")
]

class Task(models.Model):
    team_member = ForeignKey('TeamMember', related_name='tasks')
    (other fields)

how do I allow Task to be an inline to TeamMember?

Or is this only possible if TeamMember is a Page?

Zoosperm answered 31/3, 2016 at 15:5 Comment(1)
you should mark the answer as correct.Optician
P
18

You need to change the ForeignKey to ParentalKey. You may also need to change the TeamMember class to inherit from ClusterableModel.

@register_snippet
class TeamMember(ClusterableModel):
    name = models.CharField(max_length=80)

    panels = [
        FieldPanel('name'),
        InlinePanel('tasks', label="Team Tasks")
    ]

class Task(models.Model):
    team_member = ParentalKey('TeamMember', related_name='tasks')
    task = models.CharField(max_length=80)

    panels = [
        FieldPanel('task')
    ]
Paoting answered 31/3, 2016 at 20:13 Comment(1)
No worries! Could you mark the answer as correct? Thanks.Paoting

© 2022 - 2024 — McMap. All rights reserved.