#24589: Allow usage of widgets (and maybe others) in GCBV (through 
ModelFormMixin)
-----------------------------------------+------------------------
               Reporter:  MarkusH        |          Owner:  nobody
                   Type:  New feature    |         Status:  new
              Component:  Generic views  |        Version:  master
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 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),
     }
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24589>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.f100aa50c4e0bb179196d73a66d176c3%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to