Validating dynamically generated forms

1 view
Skip to first unread message

Lau...@gmail.com

unread,
May 9, 2007, 11:33:11 AM5/9/07
to Django users
Hi,

I have built this class:

class FormClass(forms.Form):
def __init__(self, *args, **kwargs):
super(FormClass, self).__init__(*args, **kwargs)
# Get tags and their id
for t in Item.objects.get(pk=id).tags.values():
field_id = t['id'] # tag ID is the form's field name
self.fields[field_id] = forms.IntegerField(min_value=1, label
= t['name'])

This class generates a dynamic form, based on the tags attached to an
item.

if request.POST:
form = FormClass(request.POST)
if form.is_valid():
# save the information
else:
form = FormClass()

# render it all to a template

Upon entering (correct) data, the form validation throws error:
supposedly the fields are empty.

What am I doing wrong here?

Many thanks in advance,

Laundro

Reluctan...@gmail.com

unread,
May 10, 2007, 11:58:46 AM5/10/07
to Django users
I didn't examine your code but I suggest using form_for_instance or
form_for_model to generate a dynamic form from a django app model.

dbal...@gmail.com

unread,
May 10, 2007, 1:43:01 PM5/10/07
to Django users
The problem appears to be because you are using a numeric key as field
name.
change this: field_id = t['id'] to field_id = str(t['id']) , or
use a real string as the name.


Reply all
Reply to author
Forward
0 new messages