#24589 closed New feature (wontfix)
Allow usage of widgets (and maybe others) in GCBV (through ModelFormMixin)
Reported by: | Markus Holtermann | Owned by: | nobody |
---|---|---|---|
Component: | Generic views | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Markus | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Generic class based views allow for fast development of basic views. Today I ran into a situation where I had to set a PasswordInput
on a form field. But the only solution I found was creating an actual ModelForm
and specifying it as form_class
on the view.
I then created a patch that allows setting the widgets through the view:
-
django/views/generic/edit.py
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index 2a25902..473f87c 100644
a b class ModelFormMixin(FormMixin, SingleObjectMixin): 120 120 A mixin that provides a way to show and handle a modelform in a request. 121 121 """ 122 122 fields = None 123 form_widgets = None 123 124 124 125 def get_form_class(self): 125 126 """ … … class ModelFormMixin(FormMixin, SingleObjectMixin): 150 151 "the 'fields' attribute is prohibited." % self.__class__.__name__ 151 152 ) 152 153 153 return model_forms.modelform_factory(model, fields=self.fields) 154 return model_forms.modelform_factory( 155 model, fields=self.fields, widgets=self.get_form_widgets() 156 ) 154 157 155 158 def get_form_kwargs(self): 156 159 """ … … class ModelFormMixin(FormMixin, SingleObjectMixin): 161 164 kwargs.update({'instance': self.object}) 162 165 return kwargs 163 166 167 def get_form_widgets(self): 168 return self.form_widgets 169 164 170 def get_success_url(self): 165 171 """ 166 172 Returns the supplied URL.
Usage:
class SomeCreateView(UpdateView): model = SomeModel fields = ['field1', 'field2', 'field3'] form_widgets = { 'field3': forms.PasswordInput(render_value=True), }
Change History (3)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Feel free to raise it on the DevelopersMailingList for some more opinions.
Note:
See TracTickets
for help on using tickets.
I don't think the possibility of saving a few lines of user code justifies the complexity of reimplementing the parameters to
modelform_factory()
as CBV parameters and methods.