Opened 5 years ago

Closed 5 years ago

Last modified 11 months 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: master
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): 
    120120    A mixin that provides a way to show and handle a modelform in a request.
    121121    """
    122122    fields = None
     123    form_widgets = None
    123124
    124125    def get_form_class(self):
    125126        """
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    150151                    "the 'fields' attribute is prohibited." % self.__class__.__name__
    151152                )
    152153
    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            )
    154157
    155158    def get_form_kwargs(self):
    156159        """
    class ModelFormMixin(FormMixin, SingleObjectMixin): 
    161164            kwargs.update({'instance': self.object})
    162165        return kwargs
    163166
     167    def get_form_widgets(self):
     168        return self.form_widgets
     169
    164170    def get_success_url(self):
    165171        """
    166172        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 Changed 5 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 5 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