Calendar events - exceeding the limitations of Wagtail/Django and considering polymorphic models

425 views
Skip to first unread message

Robin Eriksen

unread,
Apr 17, 2016, 1:46:09 PM4/17/16
to Wagtail support
I am integrating Google calendar data with my Wagtail site, solving the problem of modeling events and event instances if the events has recurrence.

Goals:


  1. The ability to list upcoming events for all available calendars.
  2. Display pages for the individual events with a listing of the upcoming instances for the event.

Original idea

My first idea was to model the events and instances as the same model, where one can discern from the attributes if the entry is an event or an instance of an event. This works quite well, since this reflects the Google Calendar API schema perfectly.

Problems


  • I only want the events to be pages, not having a huge number of pages for the event instances which will never be displayed anyway.
  • I am creating event registration forms, where I use a PageChooserPanel to select the event. Having all the instances modeled as a Page will clutter the selection widget with redundant entries, which are impossible to discern from the pertinent entries.

Alternative idea

Use two models for Event and EventInstance. This solves both problems of the original idea.

Problems

  • Querying upcoming events becomes really hard, since I use two different models and QuerySet's only allow for one model.

Considered solutions


  1. Using Polymorphic models where both Event and EventInstance inherits from a base class CalendarEntry, but I have no idea how well this will work with Wagtail and if this option will bite me hard in a future by painting myself into a corner.
  2. Some really hacky and messy querying to get upcoming events from the dual model solution, where you have to traverse all instances, include events and sort by date. A last resort, unless there's some elegant query technique, currently unknown to me.
  3. Go with the original idea, and live with the redundant Page's and hope that Wagtail will introduce a feature for allowing the PageChooserPanel to use a QuerySet instead of a model.

I really feel that I have struck a solid wall, so I would greatly appreciate any help from experienced Wagtail users, to see if this wall is as solid as I perceive it to be.

Thank you.

Matthew Westcott

unread,
Apr 18, 2016, 3:18:42 PM4/18/16
to wag...@googlegroups.com
Hi Robin,

Going with your alternative idea - it sounds like the model you have in mind is:
* a one-time-only event is represented as an Event record;
* a recurring event is represented as one Event record linked to as many EventInstance records as there are repeats of the event.

What if you change that slightly, so that a one-time event is represented as an Event record with one EventInstance - would that help? That way, when you're retrieving upcoming events, you only have to query the EventInstance table.

(Pondering on how Google Calendar does this. I wonder if they have an EventInstance model at all, given that it's possible to set up events that repeat every week indefinitely - presumably they don't create an infinite number of EventInstance records at that point :-) I'm guessing they just have the Event model, and encode the rules for repeating events within that - which would make querying for upcoming events more complex, but still doable in a SQL query - possibly even through the Django ORM.)

Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages