#28531 closed Bug (duplicate)
MultipleChoiceField / CheckboxSelectMultiple values linked to a CharField not restored on bound model forms
Reported by: | Benjamin | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm building the 'edit' page of my form. This page is supposed to show form with the data that was saved, so the form is pre populated with saved data. It works fine for most if the fields, but I have a problem with 'MultipleChoiceField' / 'CheckboxSelectMultiple' values that don't get restored. So instead of having the corresponding checkboxes checked with data from the saved form, they are all unchecked. This also affects 'SelectMultiple'
Could this be a bug ?
# 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>
Change History (3)
follow-up: 3 comment:2 by , 7 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Summary: | MultipleChoiceField / CheckboxSelectMultiple values not restored on bound forms → MultipleChoiceField / CheckboxSelectMultiple values linked to a CharField not restored on bound model forms |
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
.
comment:3 by , 7 years ago
Replying to 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 aCharField
.
It works fine with postgres array field: values are properly stored and restored.
Remark: this is probably because multiple values stored in database field as retrieved as