Tuesday, April 9, 2013

Re: Adding manytomany field, like "days of the week".

On Tue, Apr 9, 2013 at 1:57 PM, Tom Evans <tevans.uk@googlemail.com> wrote:
> On Tue, Apr 9, 2013 at 1:38 PM, Mark London <markrlondon@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

Actually, I'm completely wrong :)

Your custom form field is actually supplying the correct values - the
same as a ModelMultipleChoiceField would. However, if you were using a
ModelMultipleChoiceField, you would have seen the actual problem - you
haven't added your days of the week to the database, and so although
values are being persisted, they don't actually correspond to anything
in the Days model, and so nothing is displayed.

You cannot specify values for specific instances of a model in
models.py, instead you put the data into what django calls a fixture.
There is a special kind of fixture called 'initial_data'. When you run
syncdb, django loads any fixtures from your apps called intial_data
into the database.

https://docs.djangoproject.com/en/1.5/howto/initial-data/

You can keep specifying DAYS_OF_WEEK if you want, but Django will by
default drive everything from the database - it has no concept of
persistent lookup tables that are loaded at startup. For consistency,
you may as well drop DAYS_OF_WEEK, so there is no chance it is
inconsistent with the database.

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+unsubscribe@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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate