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,
)
Here's whats happening in my view.
When I initialize it like this:
data = {some other fields...
'privacy' : user_profile.privacy_options
}
form=ModifyProfileForm(data)
Then I show it in the template:
{{ form.privacy.label_tag }}
{{ form.privacy }}
{{ form.privacy.errors }}
I get this error:
Caught an exception while rendering: 'ManyRelatedManager' object is
not iterable
So I change the template like so:
{% for privacy in form.privacy.all %}
{{privacy}}
{% endfor %}
and I get this in my browser:
Privacy
* Enter a list of values.
as if it is not displaying any checkboxes because none have a value
set in them yet.
I want to display all checkboxes, and then check the ones which are
set.
I commented out this change in the view, no longer initializing the
form:
#'privacy' : user_profile.privacy_options,
and I still see nothing in my browser, unless I change the template to
this:
{{ form.privacy }}
Then, at least I see all checkboxes:
Privacy
o Show My Profile Page
o Show Expertise
o Show Affiliations
o Show Organization
o Show Contact Info
o Allow Messages
Now, let me uncomment the init again, and try to initialize this form
to the db values:
'ManyRelatedManager' object is not iterable error again.
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
The selected options never show up.
I know the selections are being passed to a user_profile instance and
stored in the database. So maybe this is a template rendering issue?
Thanks again,
Gloria