def get_form(self, *args, **kwargs): """ Get a ModelForm with our own `__init__` and `clean` methods. However, we need to allow ModelForm's metaclass_factory to run unimpeded, so dynamically override the methods rather than subclassing. """ # Get the vanilla modelform class ModelForm = super(GeoModelAdmin, self).get_form(*args, **kwargs) # enclose klass.__init__ orig_init = ModelForm.__init__ def new_init(self, *args, **kwargs): orig_init(self, *args, **kwargs) fix_initial_data(self.initial, self.initial_data_keymap) # enclose klass.clean orig_clean = ModelForm.clean def new_clean(self): orig_clean(self) fix_cleaned_data(self.cleaned_data, self.initial_data_keymap) return self.cleaned_data # Override methods ModelForm.__init__ = new_init ModelForm.clean = new_clean # Rearrange fields ModelForm.initial_data_keymap = apply_maps_to_modelform_fields( ModelForm.base_fields, self.maps, self.options, self.map_template, default_field_class=self.default_field_class) return ModelForm
def get_form(self, *args, **kwargs): """ Get a ModelForm with our own `__init__` and `clean` methods. However, we need to allow ModelForm's metaclass_factory to run unimpeded, so dynamically override the methods rather than subclassing. """ # Get the vanilla modelform class ModelForm = super(GeoModelAdmin, self).get_form(*args, **kwargs) # enclose klass.__init__ orig_init = ModelForm.__init__ def new_init(self, *args, **kwargs): orig_init(self, *args, **kwargs) fix_initial_data(self.initial, self.initial_data_keymap) # enclose klass.clean orig_clean = ModelForm.clean def new_clean(self): orig_clean(self) fix_cleaned_data(self.cleaned_data, self.initial_data_keymap) return self.cleaned_data # Override methods ModelForm.__init__ = new_init ModelForm.clean = new_clean # Rearrange fields ModelForm.initial_data_keymap = apply_maps_to_modelform_fields( ModelForm.base_fields, self.maps, self.options, self.map_template, default_field_class=self.default_field_class) return ModelForm