Nested categories/InlinePanel(s) in wagtail
Asked Answered
D

1

8

I struggle to implement something like "nested categories":

PageA:
  - Cat1
    - SubCat1
    - SubCat2
    - ...
  - Cat2
    - SubCat1
  - ...

All categories and subcategories should be orderable and editable by an editor.

My guess was something like this:

class CategoryTestPage(Page):
    content_panels = Page.content_panels + [
        InlinePanel('categories')
    ]


class Category(Orderable,ClusterableModel,models.Model):
    page = ParentalKey(CategoryTestPage, related_name='category')
    category = models.CharField(max_length=250)

    def __str__(self):
        return "%d %s" % (self.id, self.category)

    panels = [
            FieldPanel('category'),
            InlinePanel('subcategory')
    ]

class SubCategory(Orderable,models.Model):
    category = ParentalKey(ProjektOrdnung, related_name='subcategory')
    subcategory = models.CharField(max_length=250)

    def __str__(self):
        return "%d %s" % (self.id, self.subcategory)

    panels = [
            FieldPanel('subcategory')
    ]

But this results in 'CategoryForm' object has no attribute 'formsets'. It seems nested InlinePanels are the problem?

Further I need this "hierarchical taxonomy" for assigning some of these categories/subcategories to other pages:

PageB:
    - has Cat1
      - has SubCa2
    - ...

... which looks a lot like hierarchical tags...

Any ideas how to implement this or what's wrong with my implementation?

Kind regards, tombreit

PS: I'm on wagtail 1.2rc1

Deoxyribonuclease answered 11/11, 2015 at 13:37 Comment(0)
F
6

Here's one way to do it, with much room for interface improvements ;) In order to sort the categories at the page level, I'd suggest the use of django-sortedm2m.

from wagtail.wagtailcore.models import Orderable, Page
from wagtail.wagtailsnippets.models import register_snippet
from django.db import models


@register_snippet
class Category(models.Model):
    name = models.CharField(
        max_length=80, unique=True, verbose_name=_('Category Name'))
    slug = models.SlugField(unique=True, max_length=80)
    parent = models.ForeignKey(
        'self', blank=True, null=True, related_name="children",
        help_text=_(
            'Categories, unlike tags, can have a hierarchy. You might have a '
            'Jazz category, and under that have children categories for Bebop'
            ' and Big Band. Totally optional.')
    )
    description = models.CharField(max_length=500, blank=True)

    class Meta:
        ordering = ['name']
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")

    panels = [
        FieldPanel('name'),
        FieldPanel('parent'),
        FieldPanel('description'),
    ]

    def __str__(self):
        return self.name

    def clean(self):
        if self.parent:
            parent = self.parent
            if self.parent == self:
                raise ValidationError('Parent category cannot be self.')
            if parent.parent and parent.parent == self:
                raise ValidationError('Cannot have circular Parents.')

    def save(self, *args, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)
        return super(Category, self).save(*args, **kwargs)


class CategoryPage(models.Model):
    category = ParentalKey('Category', related_name="+", verbose_name=_('Category'))
    page = ParentalKey('MyPage', related_name='+')
    panels = [
        FieldPanel('category'),
    ]


class MyPage(Page):
    categories = models.ManyToManyField(Category, through=CategoryPage, blank=True)
    content_panels = Page.content_panels + [
        FieldPanel('categories'),
    ]
Facelift answered 29/11, 2015 at 6:37 Comment(4)
But I think this requires the snippet to be created first. Is there a way to do the creation of the Category also inline ?Lattice
I tried it this way, but as you (@ianprice) noted, the interface part leaves a lot to be desired. There's a repository from torchbox (github.com/torchbox/wagtail-categories), but unfortunately without any progress. Another approach could be wagtailplus.readthedocs.io/en/latest/relationships (not tried yet).Deoxyribonuclease
There seems to be a categories-approach encouraged by wagtail upstream authors on the way: github.com/wagtail/wagtail/pull/3305, should be available for wagtail >= 1.9.Deoxyribonuclease
The currently (October 2018) most promising approach seems to be explained by LB (Ben Johnston) in "Building a configurable taxonomy in Wagtail (Django)" in codementor.io/lb0/…Deoxyribonuclease

© 2022 - 2024 — McMap. All rights reserved.