I have a model where I store the recurring sessions on my gym.
DAYS_OF_WEEK = (
(0, _("Monday")),
(1, _("Tuesday")),
(2, _("Wednesday")),
(3, _("Thursday")),
(4, _("Friday")),
(5, _("Saturday")),
(6, _("Sunday")),
)
class RecurringSession(models.Model):
session = models.ForeignKey(SessionType, db_index=True, on_delete=models.CASCADE)
dayofweek = models.PositiveSmallIntegerField(choices=DAYS_OF_WEEK)
time = models.TimeField()
However some classes happen more than once a week. Whats the best way to store multiple values (between 1 to 7) on that modelfield? I'm using Django Mysql 8.0, and from what I understand it supports JSON field but from the django docs it seems that is only supported by Postgres so far. Is there a specific django way to do this? Maybe storing a charfield with comma-separated values? I wont need to search by that field.
Every week, there will be a cronjob that will read this model and create an instance of each session on another model. (I need each session as an instance for customization: teacher substitution, session roster, session status - i may cancel a single session on a bank holiday or for another reason). So this "RecurringSessions" will be where I store the session information that will be generated every week. The way that I wrote at first there will be an model instance for each day of the week of the same session, but I want to group them in the same instance because I think its looks nicer and may make it easier to set the teacher in bulk. Alternatively, I could store a json with a dict for multiple day/time values for each session, however that would make it a little bit more difficult to make sure that if the cronjob is run twice the class doesnt get duplicated.
Now, to avoid that, I have a UniqueConstraint in my schedule model with fields "day", "time" and "room"
Using Django 3.0.6, Python 3.8.2, and MySQL 8.0.20.
thanks