Hi,

> 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]())

If you replaced that setattr call with the following, it should work:
    self.fields[k] = field_from_type[v]()

-Rajesh D
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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