On Mon, Mar 2, 2009 at 9:03 AM, GRoby <gregory.r...@gmail.com> wrote:
> > Hello Martin, > > I had a similar need and did the following: > > Use the Dynamic Django form class from > http://www.djangosnippets.org/snippets/714/ > > An Example of how the form is built in my view (just a cleaned up C&P > snippet from my code): > > FormDictionary=BuildForm(NumberOfSpecialFields) > myForm=DynForm() > myForm.setFields(FormDictionary) > myForm.setData(request.POST) > data={} #move data to a regular dictionary so it can be > changed on this postback > for i in myForm.data.keys(): > data[i] = myForm.data[i] > myForm.data = data > > BuildForm Creates a Dictionary with all of the form fields and returns > it, for example: > Dict={} > for i in range(0, NumberOfSpecialFields): > Dict['VariableIndicator' + str(i)] = forms.BooleanField > (required=False) > Dict['VariableName + str(i)] = forms.CharField > (required=False, widget=forms.TextInput(attrs={'size':'2.5'})) > ..... > return Dict > > > This method has worked for me so far, I doubt that it would be > recommended for anything resembling a high volume site. > > Greg > > On Mar 2, 5:28 am, Martin Winkler <m...@agamisystems.eu> wrote: > > Hi all, > > > > I want to create a form with dynamic fields. That is, I want to add the > > fields using the __init__ method of my form: > > > > 1 class MyForm(forms.Form): > > 2 def __init__(self, fieldnames=(), *args, **kw): > > 3 for fieldname in fieldnames: > > 4 self.XXXfieldnameXXX = forms.CharField(...) > > 5 super(MyForm, self).__init__(*args, **kw) > > > > What is the correct syntax for line 4? Is this even possible? > > > > Martin > > > I reccomend you read this post: http://www.b-list.org/weblog/2008/nov/09/dynamic-forms/ Alex -- "I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire "The people's good is the highest law."--Cicero --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---