Wagtail GenericForeignKey and ParentalKey

355 views
Skip to first unread message

Jordan Caras

unread,
Nov 2, 2016, 9:53:43 AM11/2/16
to Wagtail support

Hi folks,

So far, wagtail has been excellent. I've come across one situation that I can't figure out. I've been reading through all of the docs and examples I can find. I suspect the answer lies in GenericForeignKeys and ContentType... but it's beyond me.

I am trying to create a Sequence of Foos and Bars with Foo and Bar being different models, but treated as one. I want to be able to add and order Foos and Bars (intermixed) on my Sequence creation page. The solution below maintains two separate orderable lists. Am I missing something obvious, or is something like this just not supported?

class Sequence(Page):
    """ A sequence of foos and bars """
    content_panels = [
        FieldPanel("title", classname="full title"),
        InlinePanel("sequence_foos", label="Foos", min_num=1),
        InlinePanel("sequence_bars", label="Bars", min_num=1),
    ]


class Foo(models.Model):
    pass

class FooSection(Orderable, Foo):
    body = RichTextField()

    sequence = ParentalKey("home.Sequence", related_name="sequence_foos", on_delete=models.PROTECT)
    panels = [
        FieldPanel("body")
    ]


class Bar(models.Model):
    pass

class BarSection(Orderable, Bar):
    title = models.CharField(max_length=250)

    sequence = ParentalKey("home.Sequence", related_name="sequence_bars", on_delete=models.PROTECT)
    panels = [
        FieldPanel("title")
    ]

My initial attempt at solving this problem just allowed my content creator to use StreamFields... but I really do have 2 separate models with separate validation rules, etc.

Thank you for any input and help!

Jordan Caras

unread,
Nov 2, 2016, 10:48:26 AM11/2/16
to Wagtail support
UPDATE:
I managed to at least figure out how to use Generic relations in my models, but the InlinePanel is exploding when trying to render it. I'm not surprised. I suspect the ModelCluster plugin which is used to render the inline panel isn't aware of the GenericRelation plugin. Would a custom form solve this? Any advice?

Jordan Caras

unread,
Nov 17, 2016, 5:12:48 PM11/17/16
to Wagtail support
Another update. After spending a day trying to figure out how to combine various panel types to do this, we decided just to take advantage of Wagtail's page hierarchy's to accomplish the same.
Reply all
Reply to author
Forward
0 new messages