tickets: 10874
This data as json
id | created | changetime | last_pulled_from_trac | stage | status | component | type | severity | version | resolution | summary | description | owner | reporter | keywords | easy | has_patch | needs_better_patch | needs_tests | needs_docs | ui_ux |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
10874 | 2009-04-20 09:57:02 | 2022-03-01 11:07:07 | 2022-03-06 03:48:28.495805 | Accepted | new | Forms | New feature | Normal | 1.0 | ModelFormMetaclass does not provide easy way of extending | 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 }}} | nobody | wombat | 0 | 0 | 0 | 0 | 0 | 0 |