# How to update form with MultipleChoiceField?

**URL:** <https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574>\
**Category:** Forms & APIs\
**Created:** [May 22, 2020, 1:52pm UTC](https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574 "2020-05-22T13:52:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex7alves](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@alex7alves](https://forum.djangoproject.com/u/alex7alves)\
**Post date:** [May 22, 2020, 1:52pm UTC](https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574/1 "2020-05-22T13:52:25Z")

</div>

When I want to send data to be updated just do:

```
def MyView(request, pk):
    instance = get_object_or_404(Mymodel, id=pk)
    form = Myform(request.POST or None, instance=instance)

```

But when I do this for a form that has a single field of multiple choices (MultipleChoiceField) it goes wrong.

forms.py

```
class FormChoices(forms.Form):
    result = forms.MultipleChoiceField(
                            choices=(MyModel.objects.values_list("id","name")),
                                    widget=forms.CheckboxSelectMultiple(),
                 label="Select")

```

views.py

```
def MyViewChoices(request, pk):

     instance = MyModel.objects.values_list('id','name' ).filter(id=pk)
     form = FormChoices(request.POST or None, instance=instance)

```

I get the following error:

```
__init__ () got an unexpected keyword argument 'instance'

```

How can I send data to this form (FormChoices) to show all items and those that have been registered appear marked?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 22, 2020, 4:44pm UTC](https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574/2 "2020-05-22T16:44:05Z")

</div>

`instance` is only for `ModelForm` classes. You want `initial` to set the initial data for a field on a normal form.

---

<div class="post-metadata">

**Author:** ![alex7alves](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@alex7alves](https://forum.djangoproject.com/u/alex7alves)\
**Post date:** [May 22, 2020, 5:20pm UTC](https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574/3 "2020-05-22T17:20:46Z")

</div>

Thank you, I didn’t know that difference.

I modified the view like this:

```auto
def MyViewChoices(request, pk):

     instance = MyModel.objects.values_list('id','name' ).filter(id=pk)
     form = FormChoices(request.POST or None, initial={'result': instance})

```

Now the template is render. But the checkboxes don’t appear marked with the sent items.

My objective is that sent items appear marked among all available items. It is possible?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [May 22, 2020, 8:18pm UTC](https://forum.djangoproject.com/t/how-to-update-form-with-multiplechoicefield/2574/4 "2020-05-22T20:18:24Z")

</div>

The only way that _I_ know of to do this is in your ` __init__ ` method in your form.

You’ve got two different things you’re trying to do here, one is to get the list of all available choices, the other is to identify the ones previously selected. You want to ensure that the form field is defined for all available choices, then modify that field to show which ones are already selected.

For a greatly simplified example:

```auto
class FormChoices(forms.Form):
    result = forms.MultipleChoiceField(
                            choices=(MyModel.objects.values_list("id","name")),
                                    widget=forms.CheckboxSelectMultiple(),
                 label="Select")
    ...
    def __init__ (self, *args, **kwargs):
        ...
        super(). __init__ (*args, **kwargs)
        ....
        # Some query to get the list of IDs to mark as selected
        ...
        self.initial['result'] = <list of ids to mark as selected>

```
