Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#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)

comment:1 Changed 5 years ago by Benjamin

Remark: this is probably because multiple values stored in database field are retrieved as

 '[\\'advisor\\', \\'member\\']'

which the bound form cannot understand when it restores values.

Last edited 5 years ago by Benjamin (previous) (diff)

comment:2 Changed 5 years ago by Tim Graham

Resolution: duplicate
Status: newclosed
Summary: MultipleChoiceField / CheckboxSelectMultiple values not restored on bound formsMultipleChoiceField / 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 in reply to:  2 Changed 5 years ago by Benjamin

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 a CharField.

It works fine with postgres array field: values are properly stored and restored.

Note: See TracTickets for help on using tickets.
Back to Top