On Sun, Nov 29, 2009 at 8:30 PM, Gloria <strang...@comcast.net> wrote:
> Here's the line from my model: > class UserProfile(models.Model): > some other fields... > privacy_options = models.ManyToManyField(PrivacyOption, > blank=True, null=True, db_table = 'usr_privacy_selection') > > Here's the bit from my form: > > class ModifyProfileForm(forms.Form): > some other fields... > privacy = forms.ModelMultipleChoiceField( > queryset=PrivacyOption.objects.all(), > required=False, > show_hidden_initial=True, > widget=forms.CheckboxSelectMultiple, > ) > Hmmm... what are you trying to do by passing the queryset? Doesn't that send your form everybody's data, instead of just the active user? If I recall correctly, passing a queryset will initialize a form, so perhaps all you need to do there is make it a "get" that returns only the current user's data? > When I initialize it like this: > > data = {some other fields... > 'privacy' : user_profile.privacy_options > } > > form=ModifyProfileForm(data) > That doesn't look right. I think should be: form=ModifyProfileForm(initial=data) But I'm also not sure if your data dict is correct. It should have an item for each field that you want to initialize. > > Trying to set the initial value for this particular field also does > not seem to help: > > initial_dict = {} > for x in user_profile.privacy_options.all(): > initial_dict[x.name]=True > > form=ModifyProfileForm(data) > form.fields['privacy'].initial = initial_dict > > Again, I don't quite understand why your queryset is using "all" - seems like you would just want one row ("get"). I'm not sure what the form object would do with a queryset that has multiple rows. Your dict should be passed in the second to last line: form=ModifyProfileForm(initial=initial_dict) Something like your last line might work, but it is a lot more work than just passing the form the initialization dict. Others feel free to jump in here and set me straight as needed. I'm still fairly new to Django, so I'm no expert... but I've been struggling to learn the same stuff lately. Nick -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.