﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24589	Allow usage of widgets (and maybe others) in GCBV (through ModelFormMixin)	Markus Holtermann	nobody	"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:

{{{#!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),
    }
}}}"	New feature	closed	Generic views	dev	Normal	wontfix		Markus	Unreviewed	0	0	0	0	0	0
