Tuesday, March 3, 2015

Can't make ManyToMany form fields hidden

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/87lhjdjpay.fsf%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment