On Tue, Apr 9, 2013 at 1:38 PM, Mark London <markrlon...@gmail.com> wrote: > Hi - I'm a django newbie, so sorry if I'm doing something stupid (I did try > to read the documentation. > > I want to have a model, that has a manytomany field that stores days of the > week, selected via a checkbox widget. I read on the web, that I could put > something like this in the model. > > DAYS_OF_WEEK = ( > ('1', 'Monday'), > ('2', 'Tuesday'), > ('3', 'Wednesday'), > ('4', 'Thursday'), > ('5', 'Friday'), > ('6', 'Saturday'), > ('7', 'Sunday'), > ) > > class Days(models.Model): > days = models.CharField(max_length=1, choices=DAYS_OF_WEEK) > > The part of the model that references the days: > > class Preferences(models.Model): > mail_days = models.ManyToManyField(Days) > > The part of the form where the days are selected, is here: > > DAYS_OF_WEEK = ( > ('1', 'Monday'), > ('2', 'Tuesday'), > ('3', 'Wednesday'), > ('4', 'Thursday'), > ('5', 'Friday'), > ('6', 'Saturday'), > ('7', 'Sunday'), > ) > > mail_days = forms.MultipleChoiceField(required=False, > widget=CheckboxSelectMultiple, choices=DAYS_OF_WEEK) > > The form displays correctly, i.e. checkboxes for the days of the week > appear. When I select the boxes, and hit submit, everything looks ok. But if > I redisplay the form , the checkboxes are all empty. But changes to the > other form fields, that I made, get saved properly (As an aside, I'm using > form.save(), not using commit=False). So why do my checkboxes selections > disappear? >
Show your full form class. The issue is that the form field should be a ModelMultipleChoiceField, since it is not, the data that is returned does not match what the model is expecting, and nothing is persisted. This does not produce an error, as the field is not required. Anyway, show your full form class so we can see what it is you are trying to do. Using a ModelForm and not overriding the mail_days form element should work, as should specifying a ModelMultipleChoiceField as the override. Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.