I want to build a system using Django that will allow users to build forms, store them and have their customers use them. I know how I can go about creating the forms dynamically but I’m looking for a good way to still use the form classes and handle many different user’s dynamic forms in an elegant way.
I’m thinking of storing the form field information as a dictionary in the db. Is there any way in django to re-interpret this dictionary back into a form object? Or do I have to write a routine that will simply interpret and build out a form in html myself?
If someone knows where to point me to the info I’d really appreciate it.
This is a Python question more than a Django question, whence my tag edit.
To reproduce the equivalent of, say:
you need something like:
where
dis a dictionary like:Of course, in the end the form class will be bound to name
f(you can usesetattrto bind it to qualified namesomething.MyFormfor an appropriatesomething, but please don’t even dream of binding it to a bare name that’s dynamically variables — it would also be a nightmare to use such a barename!).So, to recreate the form class object at runtime, you need to preserve:
MyFormand to make point 2 work, you similarly need to preserve for each field (besides its name) the type name (so you can recover the type with a
getattrfromforms) and its named parameters (as a dict), so you can basically dod[fieldname] = getattr(forms, fieldtypename)(**fieldparameters)
based on strings
fieldnameandfieldtypenameand dictfieldparameters, for each field, as you rebuild dictionaryd(i.e., prepare to satisfy step 2 of the above short list;-).