Opened 7 years ago

Closed 7 years ago

Last modified 4 years ago

#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


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/

    diff --git a/django/views/generic/ b/django/views/generic/
    index 2a25902..473f87c 100644
    a b class ModelFormMixin(FormMixin, SingleObjectMixin): 
    120120    A mixin that provides a way to show and handle a modelform in a request.
    121121    """
    122122    fields = None
     123    form_widgets = None
    124125    def get_form_class(self):
    125126        """
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    150151                    "the 'fields' attribute is prohibited." % self.__class__.__name__
    151152                )
    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            )
    155158    def get_form_kwargs(self):
    156159        """
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    161164            kwargs.update({'instance': self.object})
    162165        return kwargs
     167    def get_form_widgets(self):
     168        return self.form_widgets
    164170    def get_success_url(self):
    165171        """
    166172        Returns the supplied URL.


class SomeCreateView(UpdateView):
    model = SomeModel
    fields = ['field1', 'field2', 'field3']                
    form_widgets = {
        'field3': forms.PasswordInput(render_value=True),

Change History (3)

comment:1 Changed 7 years ago by Tim Graham

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.

comment:2 Changed 7 years ago by Tim Graham

Resolution: wontfix
Status: newclosed

Feel free to raise it on the DevelopersMailingList for some more opinions.

Note: See TracTickets for help on using tickets.
Back to Top