{{{
# forms.py
class MemberForm( forms.ModelForm ):
# ......
MODEL_CATEGORIES = (
('advisor', 'advisor'),
('member', 'member'),
('admin', 'admin'),
)
model_categories = forms.MultipleChoiceField(
widget = forms.CheckboxSelectMultiple,
choices = MODEL_CATEGORIES
)
class Meta:
model = Member
fields = [ 'model_categories' ]
# model
class Member( models.Model ):
model_categories = models.CharField(
max_length = 255,
null = True,
blank = True )
# Controller
def profile_edit_form( request ):
user = request.user or None
# Get user member profile instance
instance = get_object_or_404( Member, author = user )
form = MemberForm( request.POST or None, instance = instance )
context = {
"form" : form,
"instance": instance
}
if form.is_valid():
# ...
return redirect( 'profile_display' )
else:
# Initial form display, and redisplay of invalid form
return render( request, 'profile_edit_form_view.html', context )
# Template
<form action="/accounts/profile-edit-form/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit"/>
</form>
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28531>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Benjamin):
Remark: this is probably because multiple values stored in database field
as retrieved as
{{{
'[\\'advisor\\', \\'member\\']'
}}}
which the bound form cannot understand when it restores values.
--
Ticket URL: <https://code.djangoproject.com/ticket/28531#comment:1>
* status: new => closed
* resolution: => duplicate
Comment:
Looks like a duplicate of #27495. As far as I know, using those widgets
with `CharField` isn't supported (and shouldn't be, in my opinion). It's
better practice to store choices in a many to many table than in a
`CharField`.
--
Ticket URL: <https://code.djangoproject.com/ticket/28531#comment:2>
Comment (by Benjamin):
Replying to [comment:2 Tim Graham]:
> Looks like a duplicate of #27495. As far as I know, using those widgets
with `CharField` isn't supported (and shouldn't be, in my opinion). It's
better practice to store choices in a many to many table than in a
`CharField`.
It works fine with postgres array field: values are properly stored and
restored.
--
Ticket URL: <https://code.djangoproject.com/ticket/28531#comment:3>