Can't make ManyToMany form fields hidden

412 views
Skip to first unread message

Eric Abrahamsen

unread,
Mar 4, 2015, 12:40:40 AM3/4/15
to django...@googlegroups.com
Hi,

I'm making some heavily customized model formsets, to encourage my users
to input data. Mostly that involves cutting down the number of huge
drop-down menus they have to go surfing through.

So I have a model like this:

class Work(models.Model):
authors = models.ManyToManyField(Author,blank=True,related_name="works")

I first present them with a pre-query form, to choose an Author, then
give them a Work modelformset with the "authors" field pre-filled with
that author. So the initial data sort of looks like:

init["authors"] = [pre_query.cleaned_data["authors"]] # must be a list

for i in range(0, number_of_additional_forms):
initial.append(init)

formset = WorkFormSet(queryset=Work.objects.none(), initial=initial)

Nothing unusual there. Now I want to make the "authors" field hidden.
This is both to reassure my users, and to reduce the size of the page --
these are really long dropdowns.

Adding the hidden widget to the "authors" field, however, gives me this
validation error:

(Hidden field authors) Enter a list of values.

The form input field looks like this:

<input id="id_form-7-authors" name="form-7-authors" type="hidden" value="[37L]" />

Looks like a list of values to me, but maybe it's not getting read that
way.

In the meantime it's not a disaster if the monster "authors" dropdown is
visible for each of my Work forms, but it would be really nice to
eventually get it hidden.

In the past I did this by manually *removing* fields from the forms, and
adding the values in during the POST/save process, but that always felt
bad to me.

Thanks!
Eric

Collin Anderson

unread,
Mar 6, 2015, 7:33:57 PM3/6/15
to django...@googlegroups.com, gir...@gmail.com

Eric Abrahamsen

unread,
Mar 7, 2015, 12:25:25 AM3/7/15
to django...@googlegroups.com
Collin Anderson <cmawe...@gmail.com> writes:

> https://code.djangoproject.com/ticket/24453

For posterity: I should have been using a forms.MultipleHiddenInput
widget for the ManyToManyField, rather than a plain old
forms.HiddenInput.
Reply all
Reply to author
Forward
0 new messages