Answering to myself, I got it to work with assigning to the 'fields' and 'initial' attributes of the instantiated form. Again, I feel that this is a kind of hack and may not work with future versions of Django.
Marc On Aug 20, 11:03 am, mettwoch <mettw...@pt.lu> wrote: > Hi, > > I'd like to dynamically construct a form. Is there a way to do > something like this ... > > from django import forms > > my_form = forms.Form() > > ... and then add fields to my_form possibly along with their value. > > I've tried to add the fields directly to the fields dictionary and > values to the data dictionary of the my_form instance, but I feel > that's not the right way to do it. > > The need for this arises from the wish to allow people to edit a text > field in a structured way and thus avoiding that the fields structure > is messed up. The text field contains a varying number of lines in the > form 'field: value' and instead of letting people edit the text field > directly, giving them a form that is dynamically constructed from the > text field with a simple algorithm: > > for line in text_field.splitlines(): > field_name, sep, field_value = line.partition(':') > my_form.add_field(field=forms.CharField(name=field_name, > value=field_value)) > > The algorithm shows a fictive add_field function and instantiates the > CharField with fictive arguments. > > When getting back the POST it's easy also to reconstruct the text > field with the posted values. > > Any ideas? > > Kindly Yours > Marc --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---