I then created a patch that allows setting the widgets through the view:
{{{#!diff
diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 2a25902..473f87c 100644
--- a/django/views/generic/edit.py
+++ b/django/views/generic/edit.py
@@ -120,6 +120,7 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
A mixin that provides a way to show and handle a modelform in a
request.
"""
fields = None
+ form_widgets = None
def get_form_class(self):
"""
@@ -150,7 +151,9 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
"the 'fields' attribute is prohibited." %
self.__class__.__name__
)
- return model_forms.modelform_factory(model,
fields=self.fields)
+ return model_forms.modelform_factory(
+ model, fields=self.fields,
widgets=self.get_form_widgets()
+ )
def get_form_kwargs(self):
"""
@@ -161,6 +164,9 @@ class ModelFormMixin(FormMixin, SingleObjectMixin):
kwargs.update({'instance': self.object})
return kwargs
+ def get_form_widgets(self):
+ return self.form_widgets
+
def get_success_url(self):
"""
Returns the supplied URL.
}}}
Usage:
{{{#!python
class SomeCreateView(UpdateView):
model = SomeModel
fields = ['field1', 'field2', 'field3']
form_widgets = {
'field3': forms.PasswordInput(render_value=True),
}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24589>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by timgraham):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/24589#comment:1>
* status: new => closed
* resolution: => wontfix
Comment:
Feel free to raise it on the DevelopersMailingList for some more opinions.
--
Ticket URL: <https://code.djangoproject.com/ticket/24589#comment:2>
Comment (by Tim Graham):
[https://groups.google.com/d/topic/django-
developers/34HJqx48h6Y/discussion django-developers discussion]
--
Ticket URL: <https://code.djangoproject.com/ticket/24589#comment:3>