Hello.

I'm trying to do what I like to call "dynamically set a form's field".
I want the user to input a dictionary e.g.
{ 'intField' : 'int',
  'strField' : 'str' }
...to the constructor of the form which will then create the fields
'intField' (forms.IntegerField) and 'strField' (forms.CharField).

I have looked at Django's forms.py but I can't figure out how to do
it. This is what I have so far, which of course is not working:

class DynamicForm(forms.Form):

    def __init__(self, data):

        # The translations between types and fields.
        field_from_type = {
            'int' : forms.IntegerField,
            'float' : forms.FloatField,
            'boolean' : forms.BooleanField,
            'string' : forms.CharField,
            }

        # Dynamically add fields and stuff.
        for k, v in data.iteritems():
            setattr(self, k, field_from_type[v]())

        super(DynamicForm, self).__init__(data)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to