Defining newforms fields at runtime
|Reported by:||Owned by:||Adrian Holovaty|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||no|
I need to be able to define forms entirely at runtime (fields, choices, etc). Using form_for_xxx is fine, because it returns a class which you can manipulate using base_fields before initialising.
Rather than use type(..) which seems messy, I've added the simple function to newforms. It returns a form class based on any given form class (Form itself by default).
DynForm = forms.new_form()
DynForm.base_fieldsbla? = ...
form = DynForm()
Or for manipulating a form you've already defined elsewhere --
SpecialForm = forms.new_form(StandardForm)
SpecialForm.base_fieldsbla? = ...
form = SpecialForm()