18

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 = ?
nnachefski
  • 1,552
  • 2
  • 18
  • 29

4 Answers4

26

You may want to create DayOfTheWeek field type, which you can improve in various ways.

This code cause to translate automatically into the local language using the multilingual tools.

#myFields.py
from django.utils.translation import ugettext as _

DAY_OF_THE_WEEK = {
    '1' : _(u'Monday'),
    '2' : _(u'Tuesday'),
    '3' : _(u'Wednesday'),
    '4' : _(u'Thursday'),
    '5' : _(u'Friday'),
    '6' : _(u'Saturday'), 
    '7' : _(u'Sunday'),
}

class DayOfTheWeekField(models.CharField):
    def __init__(self, *args, **kwargs):
        kwargs['choices']=tuple(sorted(DAY_OF_THE_WEEK.items()))
        kwargs['max_length']=1 
        super(DayOfTheWeekField,self).__init__(*args, **kwargs)

#models.py
import myFields
(..)
    dayOfTheWeek = myFields.DayOfTheWeekField()
(..)
MUY Belgium
  • 2,330
  • 4
  • 30
  • 46
  • I see there are down-voters, I am quite curious to see why this do not answers the question in a efficient way. – MUY Belgium Apr 27 '20 at 08:23
  • probably because of the fact that you're importing `SmallIntegerField` yet extending a `models.CharField`. – Llanilek Jun 05 '20 at 23:26
  • I assume going this route would mean there would be nothing additional with javascript? I am working with a dropdown of days of the week and I need it to be sorted by the day of the week as I am rendering the user's data – abhivemp Nov 11 '20 at 09:43
  • @abhivemp Indeed, no javascript is required. Two files are impacted. – MUY Belgium Nov 13 '20 at 10:33
  • @MUYBelgium Do you know if it's possible to render the label of the day of the week (for instance, 'Monday') instead of '1' on the jinja template? If so, how would I go about it? – abhivemp Nov 18 '20 at 16:35
  • @abhivemp Last time I checked, nothing has to be done : the default widget used by the field `DayOfTheWeekField` did not display a number but a list of translated name of the day of the week. Only, the value of the option was a number (or a string with only one char), in the HTML code. May be you should look something simpler... `disabled='disabled'`? `something.choices[value]`? May be add a other function to the field, after `def __init__`? – MUY Belgium Nov 23 '20 at 15:16
  • @abhivemp Possibly, just the stringification of the field? – MUY Belgium Nov 23 '20 at 15:23
23

Something like this would work.

#models.py
DAYS_OF_WEEK = (
    (0, 'Monday'),
    (1, 'Tuesday'),
    (2, 'Wednesday'),
    (3, 'Thursday'),
    (4, 'Friday'),
    (5, 'Saturday'),
    (6, 'Sunday'),
)

days = models.CharField(max_length=1, choices=DAYS_OF_WEEK

#forms.py
widgets = { 'days': forms.CheckboxSelectMultiple }

Or to save multiple days

#models.py
class Days(models.Model):
    day = models.CharField(max_length=8)

days = models.ManyToManyField(Days)

#forms.py
widgets = { 'days': forms.CheckboxSelectMultiple }
silent1mezzo
  • 2,814
  • 4
  • 26
  • 46
4

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.

Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
0

Just implemented django-weekday-field. Works great! Hopefully this helps other people who stumble upon this question

EDIT: updated link to pypi since the bitbucket repo was deleted.

It hasn't been updated since 2014 but looking at the code is a great way to get started on answering this question

Regneel
  • 114
  • 4