Wagtail How to get the value of an orderable
I am a relative newcomer to programming Wagtail My attempt is a cascaded application where the title is formed from the relation to the underlying element. The cascade should be structured like this Fach --> Reihe --> Ebene --> Behaelter My models.py
from django.db import models
from django.utils.text import slugify
from wagtail.models import Page
from wagtail.admin.panels import FieldPanel # InlinePanel # PageChooserPanel
from django import forms
from wagtail.admin.forms import WagtailAdminPageForm
from django.utils.translation import gettext_lazy as _
# Create your models here.
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
class FachIndexPage(Page):
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['Fächer'] = FachPage.objects.all()
return context
subpage_types = ['FachPage']
I read the documentation on the wagtail-site I try a function with get_context and many more
You want to make a Wagtail setup where each page's title is based on the elements it's related to. If I got it right, your ReihePage should have a title made up of how many FachPage instances it has and what their titles are, correct? To do this, you need to get the related FachPage instances from your ReihePage. You can do this by querying the related FachPage instances within the ReihePage model.
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)