[Django] #29468: Form Field declaration:

11 views
Skip to first unread message

Django

unread,
Jun 3, 2018, 8:26:53 PM6/3/18
to django-...@googlegroups.com
#29468: Form Field declaration:
-------------------------------------+-------------------------------------
Reporter: Ivan | Owner: nobody
Muller |
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Keywords: Field Widget html
Triage Stage: | attrs
Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hi guys, I want to improve the way of declaring html attrs into a
Fields With out declaring the default widget again.

{{{
example:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
full_name = forms.CharField(widget=forms.TextInput(attrs={'class':
Anyclass',}))
}}}

As Field documentaiton say into the init:
"""Each Field has a default Widget that it'll use if you don't specify
this. n most cases, the default widget is TextInput:"""
Woudl be god to declare it as


{{{
full_name = forms.CharField(widget_attrs={'class': Anyclass',})
}}}

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

Django

unread,
Jun 3, 2018, 8:27:19 PM6/3/18
to django-...@googlegroups.com
#29468: Form Field declaration:
-------------------------------------+-------------------------------------
Reporter: Ivan Muller | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution:

Keywords: Field Widget html | Triage Stage:
attrs | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Ivan Muller:

Old description:

> Hi guys, I want to improve the way of declaring html attrs into a
> Fields With out declaring the default widget again.
>
> {{{
> example:
> class ProfileForm(forms.ModelForm):
> class Meta:
> model = Profile
> full_name = forms.CharField(widget=forms.TextInput(attrs={'class':
> Anyclass',}))
> }}}
>

>
> As Field documentaiton say into the init:
> """Each Field has a default Widget that it'll use if you don't specify
> this. n most cases, the default widget is TextInput:"""
> Woudl be god to declare it as
>

> {{{
> full_name = forms.CharField(widget_attrs={'class': Anyclass',})
> }}}

New description:

Hi guys, I want to improve the way of declear html attrs into a Fields


With out declaring the default widget again.

{{{
example:
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
full_name = forms.CharField(widget=forms.TextInput(attrs={'class':
Anyclass',}))
}}}

As Field documentaiton say into the init:
"""Each Field has a default Widget that it'll use if you don't specify
this. n most cases, the default widget is TextInput:"""
Woudl be god to declare it as


{{{
full_name = forms.CharField(widget_attrs={'class': Anyclass',})
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29468#comment:1>

Django

unread,
Jun 3, 2018, 8:30:32 PM6/3/18
to django-...@googlegroups.com
#29468: Form Field declaration:
-------------------------------------+-------------------------------------
Reporter: Ivan Muller | Owner: nobody

Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution:

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

Comment (by Ivan Muller):

Maybe that easy hack may help
{{{
diff --git a/django/forms/fields.py b/django/forms/fields.py
index 370f78e..af99d2e 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -55,7 +55,8 @@ class Field:
-
def __init__(self, *, required=True, widget=None, label=None,
initial=None,
help_text='', error_messages=None,
show_hidden_initial=False,
- validators=(), localize=False, disabled=False,
label_suffix=None):
+ validators=(), localize=False, disabled=False,
label_suffix=None,
+ widget_attrs={}):
# required -- Boolean that specifies whether the field is
required.
# True by default.
# widget -- A Widget class, or instance of a Widget class, that
should
@@ -100,6 +101,7 @@ class Field:
-
# Hook into self.widget_attrs() for any Field-specific HTML
attributes.
extra_attrs = self.widget_attrs(widget)
+ extra_attrs.update(widget_attrs)
if extra_attrs:
widget.attrs.update(extra_attrs)

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29468#comment:2>

Django

unread,
Jun 5, 2018, 3:00:33 AM6/5/18
to django-...@googlegroups.com
#29468: Improve Form Field declaration of widget `attrs`.

-------------------------------------+-------------------------------------
Reporter: Ivan Muller | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

Keywords: Field Widget html | Triage Stage:
attrs | Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => wontfix


Old description:

> Hi guys, I want to improve the way of declear html attrs into a


> Fields With out declaring the default widget again.
>
> {{{
> example:
> class ProfileForm(forms.ModelForm):
> class Meta:
> model = Profile
> full_name = forms.CharField(widget=forms.TextInput(attrs={'class':
> Anyclass',}))
> }}}
>

>
> As Field documentaiton say into the init:
> """Each Field has a default Widget that it'll use if you don't specify
> this. n most cases, the default widget is TextInput:"""
> Woudl be god to declare it as
>

> {{{
> full_name = forms.CharField(widget_attrs={'class': Anyclass',})
> }}}

New description:

Hi guys, I want to improve the way of declear html attrs into a Fields


With out declaring the default widget again.

Example:

{{{


class ProfileForm(forms.ModelForm):
class Meta:
model = Profile

full_name = forms.CharField(widget=forms.TextInput(attrs={'class':
Anyclass',}))
}}}

As Field documentaiton say into the init:

"""Each Field has a default Widget that it'll use if you don't specify
this. n most cases, the default widget is TextInput:"""

Woudl be god to declare it as


{{{
full_name = forms.CharField(widget_attrs={'class': Anyclass',})
}}}

--

Comment:

Hi @mullerivan,

The docs for [https://docs.djangoproject.com/en/2.0/ref/forms/widgets
/#styling-widget-instances Styling widget instances] give three
approaches. The one you'd like to avoid is just the first.

In your case, I guess I'd recommend the second approach, to adjust the
widget attrs inline in the form definition:


{{{
class ProfileForm(forms.ModelForm):
full_name = forms.CharField()

full_name.widget.attrs.update({'class': 'Anyclass',})

class Meta:
model = Profile
}}}

Given that multiple options already exist, adding yet another keyword
argument is not going to be worth it on balance.
(The benefit won't justify the extra API surface area.)

Also see the `ModelForm` docs on
[https://docs.djangoproject.com/en/2.0/topics/forms/modelforms
/#overriding-the-default-fields Overriding default fields]. If it's API
**you** really want in **your project** there's nothing to stop you
creating a base form class or mixin the takes an additional
`extra_widget_attrs` and sets that appropriately during `__init__()`, as a
generalisation of the third strategy outlined in the ''Styling widget
instances'' docs linked above.

--
Ticket URL: <https://code.djangoproject.com/ticket/29468#comment:3>

Django

unread,
Jun 5, 2018, 9:22:08 AM6/5/18
to django-...@googlegroups.com
#29468: Allow overriding a widget's attributes without redeclaring the default
widget

-------------------------------------+-------------------------------------
Reporter: Ivan Muller | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

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

--
Ticket URL: <https://code.djangoproject.com/ticket/29468#comment:4>

Django

unread,
Jun 5, 2018, 12:21:32 PM6/5/18
to django-...@googlegroups.com
#29468: Allow overriding a widget's attributes without redeclaring the default
widget
-------------------------------------+-------------------------------------
Reporter: Ivan Muller | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution: wontfix

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

Comment (by Ivan Muller):

Thanks a lot! im actually likes the way that you define to handle
this scenario

Cheers

--
Ticket URL: <https://code.djangoproject.com/ticket/29468#comment:5>

Reply all
Reply to author
Forward
0 new messages