How to store multiple values on a django model field

Viewed 3323

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

2 Answers

It's hard to say the best structure without knowing what you're ultimately going to do with the model, however, I don't see why you couldn't have a single Session model that covers everything - like so:

class Session(models.Model):
    TYPE_CHOICES = (('C','Cardio'), #...and so on
    DAY_CHOICES = ((1,'Monday'), #...and so forth

    type = models.CharField(max_length=10, choices=TYPE_CHOICES)
    day = models.PositiveSmallIntegerField(choices=DAY_CHOICES)
    time = models.TimeField()

You could find the recurring sessions by counting the occurrences of a session once you've applied a filter to type or another field. That would keep your model structure concise and give you enough information in a single place to do filtering, annotation, etc. as required.

You're attempting to do a many-to-one relationship, so you'll either have to use this method docs.djangoproject.com/en/3.0/topics/db/examples/many_to_one, or if you want to avoid creating a database table and utilize Django's choice field, you can use this extension.

Since there are seven fixed days in a week, I'll prefer the second method instead of creating a DB table, and unnecessary DB queries.

Related