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.
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()
(..)
- Django Admin + FORCE_SCRIPT_NAME + Login redirects incorrectly
- How to properly query a ManyToManyField for all the objects in a list (or another ManyToManyField)?
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 }
- Django and Celery โ ModuleNotFoundError: No module named 'celery.task'
- Django OneToOneField with possible blank field
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
- Graphene-python performance issues for large data sets
- Appropriate choice of authentication class for python REST API used by web app
- How do you use Django-filter's '__in' lookup?
- Why does Django South 1.0 use iteritems()?
- What is the right way to use angular2 http requests with Django CSRF protection?