views:

758

answers:

2

I have a Django form that uses a different number of fields based on the year/month. So I create the fields in the form like this:

for entry in entry_list:
    self.fields[entry] = forms.DecimalField([stuffhere])

but now I don't know how to get the submitted data from the form.

Normally I would do something like:

form.cleaned_data["fieldname"]

but I don't know what the names of the fields are. The debug screen shows my POST data as simply "Entry Object" with a value of "u''". Calling POST.lists() doesn't show anything.

I am sure I am missing something obvious, but I've been stuck on this for a few days too many. Is there a better way to do this? Is all of the data in the request object, but I just don't know how to use it?

Here is the code for the model/form/view: http://pastebin.com/f28d92c0e

Much Thanks!

EDIT:

I've tried out both of the suggestions below. Using formsets was definitely easier and nicer.

+5  A: 

I think you might be better off using formsets here. They're designed for exactly what you seem to be trying to do - dealing with a variable number of items within a form.

Daniel Roseman
Formsets looks like exactly what I need, thank you.
pinchyfingers
A: 

In this line:

self.fields[entry] = forms.DecimalField(max_digits=4, decimal_places=1, label=nice_label)

entry is a model instance. But fields are keyed by field names (strings). Try something like:

self.fields[entry.entry_name] = forms.Decimal(...)

(substitute appropriate for "entry_name").

ars
Thanks. I realized that just before I left for work this morning. I'm going to give formsets a try, but thanks for helping me find the error in my form, hopefully my future forms will work nicer.
pinchyfingers
This is actually the way I am fixing it right now, so far everything is working wonderfully. Thanks!
pinchyfingers
Glad it's working out. :-)
ars