Returning submitted formset data to the template for further editing

41 views
Skip to first unread message

Daniele Procida

unread,
Jun 27, 2014, 11:26:34 AM6/27/14
to Django Users
As far as I can tell, there doesn't seem to be an easy way to do this:

1. bind POST data from the request to a formset
2. loop over the forms in the formset
3. for each form, manipulate some of the form data
4. return the manipulated form data to the template
5. in the template, have useful access to the forms' is_valid(), validation and so on

Form data are immutable, so can't be edited directly, so step 3 is difficult.

We could copy the immutable form data to make them editable, but since they are in one great big soup of a QueryDict and have prefixes, picking them out one by one for each form and field to edit them is messy and unpleasant.

We could instead process all the data in the usual way, then put each dict of form.cleaned_data into a new list, and create a new formset using my_formset=MyFormset(initial=all_my_cleaned_data_dicts), but now that's unbound, which makes step 5 is impossible.

I think the answer has to be to get all of the cleaned_data dicts; loop over the keys, add the prefixes to each one; dump the key/value pairs into a new data dict; grab the formset management key/values and transfer those too, and then do my_formset.data = my_new_data_dict.

But it seems like an awful lot of work for what must be a fairly usual use-case.

Daniele

Andrew Choi

unread,
Jun 28, 2014, 9:29:35 AM6/28/14
to django...@googlegroups.com
Daniele,

It doesn't seem like there's an easier way to do what you're proposing. However, what's the form data manipulation that you need to do for some of these forms? Is there a way to separate that from the form set submission?

Andrew

Daniele Procida

unread,
Jul 2, 2014, 2:59:09 AM7/2/14
to Django Users
On Sat, Jun 28, 2014, Andrew Choi <andrewy...@gmail.com> wrote:

>It doesn't seem like there's an easier way to do what you're proposing.
>However, what's the form data manipulation that you need to do for some of
>these forms? Is there a way to separate that from the form set submission?

Each time the form is resubmitted to have the data processed, it needs to complete some fields based on others, and related data in the database.

Daniele

Andrew Choi

unread,
Jul 2, 2014, 8:36:39 AM7/2/14
to django...@googlegroups.com
Daniele,

Are you processing the formset on one page or on multiple? If multiple,
then it seems that you don't need a formset and can just chain the form
inputs into the next form.

For a single page, the first thing that comes to mind is doing the
processing client-side, via Javascript and AJAX calls. This would only
be feasible if the information that you're using to process does not
need to be secret. Then again, writing a client-side data processor
might be just as much work as writing formset validation code from scratch.

Andrew
Reply all
Reply to author
Forward
0 new messages