Saturday, June 28, 2014

Re: Returning submitted formset data to the template for further editing

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

On Saturday, June 28, 2014 12:26:34 AM UTC+9, Daniele Procida wrote:
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

--
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/21db66a8-68df-45ea-9b3b-128394462f14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment