Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter items from an InlinePanel in wagtail

Tags:

wagtail

For a small listing of events I used an InlinePanel in my page model. Now I would like to filter these events by date, like I would do when using a @property with subpages: date__gte=date.today() for only displaying the future events on the page TourdatenIndexPag. How to achieve that?

My implementation:

class EventItem(LinkFields):
    date = models.DateField("Datum")
    ...

    panels = [FieldPanel('date')]

    class Meta:
        abstract = True


class TourdatenPageEventItem(Orderable, EventItem):
    page = ParentalKey('md.TourdatenIndexPage', related_name='event_items')


class TourdatenIndexPage(Page):
    ...
    content_panels = Page.content_panels + [
        InlinePanel('event_items', label="Events"),
    ]

Where and how could these event_items be accessed and filtered?

like image 1000
tombreit Avatar asked Oct 15 '25 11:10

tombreit


1 Answers

Create a method on your page model to return the queryset you want:

class TourdatenIndexPage(Page):
    def future_event_items(self):
        return self.event_items.filter(date__gte=date.today())

Then, on your template, you can refer to self.future_event_items:

{% for event in self.future_event_items %}
    <li>{{ event.date }}</li>
{% endfor %}
like image 151
gasman Avatar answered Oct 19 '25 14:10

gasman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!