Question: I put some client-side Javascript in my template that allows a user to dynamically add fields to a form. My problem is that these fields are cleaned in `form.cleaned_data`, so I can't access them that way.

All the fields are accessible in `request.POST`, so I could just solve this problem with that, but I want to do this the "right way" and I think that the solution lies somewhere in using django forms rather than reading the request directly.

I tried overriding `form.clean()`, but it seems like the data is already gone by the time it gets there.

Other details: I am naming these fields `fieldname_x`, where `x` is a number. In `request.POST`, `request.POST['fieldname']` is a list of a all the values, but `form.cleaned_data` contains only the last value of each list.

Answer: 
Do you know what type these fields are going to be beforehand? Are they just going to be simple text fields? I've done something similar to this, creating dynamic forms.

```
# make sure these exist by examining request.POST
custom_fields = ['fieldname_1', 'fieldname_2']

attrs = dict((field, forms.CharField(max_length=100, required=False)) 
             for field in custom_fields)
DynamicForm = type("DynamicForm", (YourBaseForm,), attrs)
submitted_form = DynamicForm(request.POST)

```

Your submitted form should now contain all the fields you require, along with their values. You might want to remove required=False, but that's up to you.

What this does, is perform a dynamic subclass of your base form, adding the attributes passed in as attrs to the class definition. So when you create an instance with post data, they should be mapped correctly.

Edit:

I read the question a little more closely. What you'll want to do is ensure that your dynamic input elements are named correctly, and the values map to those fieldnames once it reaches django. Otherwise, request.POST will not fill the form in correctly.

```
<input type='text' name='fieldname_1' value='value_for_field_1' /> 

```

etc
I'm doing this in the project I'm currently working on. You'll want to have a look at formsets. But be warned, it isn't pretty. You have to modify this stupid hidden field that stores to the form count, and you have to rename the inputs correctly.. which involves a lot of pointless string parsing. If they had used arrays, like PHP, there wouldn't be an issue, but they're hell bent on making my life miserable... **rambles off**

**Edit:**
<http://docs.djangoproject.com/en/dev/topics/forms/formsets/#empty-form> they provide this which makes your life 5% less painful... you can just do a find and replace on **prefix** with the appropriate number.. but I think I had an issue where the empty form doesn't contain the proper default values if you initialize the form with post data, so I had to create a 2nd instance without post data to an actually empty empty\_form, which is just retarded.