ModelFormMetaclass does not provide easy way of extending
|Reported by:||wombat||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
I am currently creating an extension to the ModelForm that provides similar features to Admin such as fieldsets. Unfortunately, the ModelFormMetaclass does not provide easy way of extending ModelForm. For example, I need to access meta information for fieldsets and dynamicaly flatten to fields before method fields_for_model is called. Also, what if I wanted to override fields for model. There is no way of doing this without completely overriting the entire ModelFormMetaclass. It seems that the meta class approach initialization does not support granual overriding of logic. I have made the following code change to django ModelFormMetaclass to ease my pain for now. However, in the long run it may not be enought.
class ModelFormMetaclass(type): .... options_class = attrs.pop('options_class', ModelFormOptions) opts = new_class._meta = options_class(getattr(new_class, 'Meta', None)) ...
The following is the code that uses this:
class DynamicModelFormOptions(ModelFormOptions): def __init__(self, options=None): super(DynamicModelFormOptions,self).__init__(options) self.fieldsets = getattr(options, 'fieldsets', None) self.fieldwidths = getattr(options, 'fieldwidths', None) if self.fieldsets: self.fields = flatten_fieldsets(self.fieldsets) class DynamicModelFormMetaclass(ModelFormMetaclass): def __new__(cls, name, bases, attrs): attrs['formfield_callback'] = formfield_for_dbfield attrs['options_class'] = DynamicModelFormOptions new_class = super(DynamicModelFormMetaclass, cls).__new__(cls, name, bases, attrs) #new_class._meta = DynamicModelFormOptions(getattr(new_class, 'Meta', None)) #assign_default_widgets(new_class.base_fields) return new_class
Change History (5)
comment:1 Changed 6 years ago by Alex
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted