Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a form in django like google form? where user can add or delete field according to their need?User can add up to 10-20 field?

Need to create form where users can add or delete fields acc to their need like user can add email, phone no field in the form or like family info, etc or if the user doesn't want that field he can delete the field. Which Django property I can you to create this form or field.

like image 383
rajat maan Avatar asked Oct 24 '25 04:10

rajat maan


1 Answers

It's not hard to dynamically create a form, although you need something to determine what it should contain for each request. The three-argument form of (Python) type can be used. For example,

fields = {
  'foo': forms.CharField( max_len=80 )
  'bar': forms.IntegerField() 
}
MyForm = type( 'MyForm', (forms.Form, ), fields)

...
form = MyForm()

which is the same as

class MyForm( forms.Form):
    foo = forms.CharField( max_len=80 )
    bar = forms.IntegerField() 

...
form = MyForm()

except that you can construct the contents of fields dynamically, using some runtime entity to decide what fields with what names and of what type should be in the form.

like image 136
nigel222 Avatar answered Oct 26 '25 19:10

nigel222