python - Add form fields to django form dynamically -
due bd design depending on value, data stored in different cells, have add form fields dynamically. thinking on this:
class editflatform(basemodelform): on_sale = forms.booleanfield(required=false) on_rent = forms.booleanfield(required=false) class meta: model = flat fields = ('title', 'flat_category', 'description') ... def __init__(self, *args, **kwargs): super(editflatform, self).__init__(*args,**kwargs) flat_properties = flatproperty.objects.all() p in flat_properties: if p.type_value == 1: # text setattr(self, p.title, forms.charfield(label=p.human_title, required=false)) elif p.type_value == 2: # number setattr(self, p.title, forms.integerfield(label=p.human_title, required=false)) else: # boolean setattr(self, p.title, forms.booleanfield(label=p.human_title, required=false))
but fields don't added, missing?
assuming p.title
string variable, should work:
if p.type_value == 1: # text self.fields[p.title] = forms.charfield(label=p.human_title, required=false))
Comments
Post a Comment