I need to set initial data to formset
with ManyToMany
field.
Usually i'm doing like this when there is no ManyToMany field in forms of my fomset:
PersonFormSet = forms.formsets.formset_factory(NickName, can_delete=True)
init_data = [{'name':'Vasya pupkin','nick':'Vasya'},
{'name':'Vasyapupkin','nick':'Petya'}]
nick_formset = PersonFormSet(initial=init_data)
But now I need to set ManyToMany field initial data and tried something like this:
NickNameFormSet = forms.formsets.formset_factory(NickName, can_delete=True)
init_data = [{'name': 'Vasya Pupkin',
'nick': {'Vasya':'selected',
'Petya':'notselected'}}]
nick_formset = NickNameFormSet(initial=init_data)
But it doesn't works.
How can I pass initial data to Formset so it render my widget like this:
<select multiple="multiple" name="person_set-0-nickname" id="id_person_set-0-nickname">
<option value="1" selected="selected">Vasya</option>
<option value="2">Petya</option>
</select>
Note: I'm using only Forms, and Formsets of Django. There is no Django models. I can actually define it but it's empty, I'm using NoSQL
.