Search code examples
django-modelswagtailwagtail-snippet

How can I add an inline orderable model to a Wagtail Snippet?


I want to be able to add an inline orderable model to a Wagtail Snippet. The code below gives me an error saying that I have to use a ParentalKey for a ClusterableModel. Please advise.

@register_snippet
@python_2_unicode_compatible
class NavCategory(models.Model):
    title = models.CharField(max_length=200)

    panels = [
        FieldPanel('title'),
        InlinePanel('nav_item', label='Pages')
    ]

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = 'nav categories'
        ordering = ['title']


class NavItem(Orderable):
    category = ParentalKey(
        'core.NavCategory',
        related_name='nav_item'
    )
    link = models.ForeignKey(
        'wagtailcore.Page',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
    )

    panels = [
        PageChooserPanel('link')
    ]

    def __str__(self):
        return self.category.title + ' -> ' + self.nav_item.link

    class Meta(Orderable.Meta):
        verbose_name = 'Nav Item'
        verbose_name_plural = 'Nav Items'

Solution

  • NavCategory needs to inherit from modelcluster.models.ClusterableModel:

    from modelcluster.models import ClusterableModel
    
    class NavCategory(ClusterableModel):
        ...