Some suitable definitions are found in django/forms/forms.py . It:
class BaseFormclass Formclass DeclarativeFieldsMetaclassdef get_declared_fields
get_declared_fields is called from DeclarativeFieldsMetaclass and creates a list with field instances sorted by their creation counter. He then adds the fields from the base classes to this list and returns the result as an OrderedDict instance with the field name serving as keys. DeclarativeFieldsMetaclass then inserts this value into the base_fields attribute and calls type to create the class. Then it passes the class to the media_property function in widgets.py and binds the return value to the media attribute of the new class.
media_property returns a property method that reconstructs the media ads on every access. I feel that this will not be relevant here, but I could be wrong.
Anyway, if you don't declare the media attribute (and none of the base classes), it only returns a fresh instance of media with no arguments to the constructor, and I think that monkeypatching the new on field should be as simple as manually inserting the field into base_fields .
ContactForm.another_field = forms.CharField(...) ContactForm.base_fields['another_field'] = ContactForm.another_field
Then each instance of the form gets deepcopy of base_fields , which becomes form_instance.fields in the __init__ BaseForm method. NTN.
aaronasterling
source share