[Django] #24589: Allow usage of widgets (and maybe others) in GCBV (through ModelFormMixin)

16 views
Skip to first unread message

Django

unread,
Apr 6, 2015, 10:35:54 AM4/6/15
to django-...@googlegroups.com
#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.

Django

unread,
Apr 6, 2015, 12:36:35 PM4/6/15
to django-...@googlegroups.com
#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 | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

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>

Django

unread,
Apr 7, 2015, 10:51:24 AM4/7/15
to django-...@googlegroups.com
#24589: Allow usage of widgets (and maybe others) in GCBV (through ModelFormMixin)
-------------------------------+--------------------------------------
Reporter: MarkusH | Owner: nobody
Type: New feature | Status: closed

Component: Generic views | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Dec 4, 2018, 5:28:56 PM12/4/18
to django-...@googlegroups.com
#24589: Allow usage of widgets (and maybe others) in GCBV (through ModelFormMixin)
-----------------------------------+--------------------------------------
Reporter: Markus Holtermann | Owner: nobody
Type: New feature | Status: closed

Component: Generic views | Version: master
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages