wagtail 中的嵌套类别/内联面板 [英] Nested categories/InlinePanel(s) in wagtail
问题描述
我很难实现嵌套类别"之类的东西:
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.
我的猜测是这样的:
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')
]
但这会导致 'CategoryForm' 对象没有属性 'formsets'
.似乎嵌套的 InlinePanel
是问题?
But this results in 'CategoryForm' object has no attribute 'formsets'
. It seems nested InlinePanel
s 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?
亲切的问候,墓地
PS:我在 wagtail 1.2rc1 上
PS: I'm on wagtail 1.2rc1
推荐答案
这是一种方法,界面改进空间很大;) 为了在页面级别对类别进行排序,我建议使用django-sortedm2m.
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'),
]
这篇关于wagtail 中的嵌套类别/内联面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!