djangodjango-modelsmodeldays

django days-of-week representation in model


I have this "Jobs Server" model that i'm building. I want to include a field that will save which days of the week this job will run on. Ultimately in the UI, i would like the user to be able to have a series of check boxes(one for each day) that they can select. What would be the best way to represent this "days-of-week" data in my mode?

class Job(models.Model):
    name = models.CharField(max_length=32, unique=True)
    package = models.ForeignKey(Package)
    binary = models.ForeignKey(Binary)
    host = models.ForeignKey(Host)
    colo = models.ForeignKey(Location)
    group = models.ForeignKey(Group)
    type = models.ForeignKey(Type)
    start = models.TimeField()
    end = models.TimeField()
    days = ?

Solution

  • If you want a checkbox for each one, then the easiest thing to do is to create BooleanFields for each of them. If you want to store it as a more complex value (eg. comma separated list or something), create your own widget and play with javascript, then you could go that route.