Wagtail 如何获取可排序项的值
我刚开始学习使用Wagtail编程。我的尝试是做一个级联应用,也就是说标题是根据底层元素的关系来形成的。这个级联的结构应该是这样的:专业 --> 系列 --> 级别 --> 容器。
这是我的models.py文件。
class NoTitleForm(WagtailAdminPageForm):
title = forms.CharField(required=False, disabled=True, help_text=('Title is autogenerated'))
slug = forms.CharField(required=False, disabled=True, help_text=('Slug is autogenerated'))
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not self.initial['title']:
self.initial['title'] = _('auto-generated-title')
if not self.initial['slug']:
self.initial['slug'] = _('auto-generated-slug')
class ReihePage(Page):
base_form_class = NoTitleForm
content_panels = [
MultiFieldPanel(
[
InlinePanel("Fach", label="Fach", min_num=0, max_num=10),
]
),
FieldPanel('title'),
FieldPanel('slug'),
]
def clean(self):
super().clean()
new_title = "anzahl from Fach" + "title from Fach" <--** How to get the values from the InlinePanel**
self.title = new_title
self.slug = slugify(new_title)
class ReihePageOrderable(Orderable):
page = ParentalKey(ReihePage, related_name="Fach", null=True)
fach = models.ForeignKey("Fachpage", on_delete=models.SET_NULL, null=True)
anzahl = models.IntegerField(default=1)
class FachPage(Page):
laenge = models.IntegerField(null=True, blank=True, verbose_name='Länge in mm')
breite = models.IntegerField(null=True, blank=True, verbose_name='Breite in mm')
content_panels = [
# FieldPanel('bezeichnung'),
FieldPanel('laenge'),
FieldPanel('breite'),
FieldPanel('title'),
FieldPanel('slug'),
]
parent_page_types = ['FachIndexPage']
class Meta:
unique_together = ('laenge', 'breite')
def clean(self):
"""Override the values of title and slug before saving."""
super().clean()
a = str(self.laenge)
b = str(self.breite)
new_title = "Fach" + " " + a + "x" + b + " mm"
self.title = new_title
self.slug = slugify(new_title)
base_form_class = NoTitleForm
我看了Wagtail网站上的文档,尝试使用get_context函数,还有很多其他的尝试。
1 个回答
0
你想要建立一个Wagtail的设置,让每个页面的标题根据它相关的元素来决定。如果我没理解错的话,你的ReihePage应该有一个标题,这个标题是由它包含的FachPage实例的数量和这些实例的标题组成,对吧?为了做到这一点,你需要从你的ReihePage中获取相关的FachPage实例。你可以通过在ReihePage模型中查询这些相关的FachPage实例来实现。
class ReihePage(Page):
base_form_class = NoTitleForm
content_panels = [
MultiFieldPanel([
InlinePanel("fach_relation", label="Fach", min_num=0, max_num=10),
]),
FieldPanel('title'),
FieldPanel('slug'),
]
def clean(self):
super().clean()
fach_instances = self.fach_relation.all()
fach_count = len(fach_instances)
fach_titles = [fach_instance.title for fach_instance in fach_instances]
new_title = f"Count from Fach: {fach_count}, Titles: {', '.join(fach_titles)}"
self.title = new_title
self.slug = slugify(new_title)
class ReihePageOrderable(Orderable):
page = ParentalKey(ReihePage, related_name="fach_relation")
fach = models.ForeignKey("FachPage", on_delete=models.SET_NULL, null=True)
anzahl = models.IntegerField(default=1)