M2M HiddenInput field not returning valid list data

Hi all,

I’m having trouble this morning with submission of every form of a formset with a ManyToMany hidden_input widget. When rendered, there is a valid list of primary key ints in the field ‘value’.

<input type="hidden" name="n-18-6-state_space" value="[172, 175]" id="id_n-18-6-state_space">

When submitted, it fails validation and gives form.errors: "{‘state_space’: [‘Enter a list of values.’]}

This formset instantiation works for all the normal fields:
formset = NexusBRVMFormSet(request.POST, prefix="n-" + str(question.pk))

In the past, I’ve got the M2M content manually from request.POST.lists(), but my reading of the docs makes me think that shouldn’t be needed here(?)

I note in the docs that to save M2M data I should also call formset.save_m2m(), but my problem seems upstream from this.

Assuming the base form is one instance of one object and the formset are all related to that specific object (perhaps an inline formset), I don’t believe the hidden form element is necessary. The many-to-many relationship is defined by the primary keys of each related table, and that information is available from the base object and each row of the related object. The save_m2m method call is only required when you do a form.save(commit=False). (In any event, this should be something easy enough to try.)

1 Like