formfield_overrides doesn't work

443 views
Skip to first unread message

Tim Johnson

unread,
May 11, 2019, 3:10:06 PM5/11/19
to Django ML
django 2.1.5 with python 3.7.2

I have a models.py class as follows:
class Article(models.Model):
title = models.CharField(max_length=255,)

And I want to override the rendered default size attribute to 100 in
the input/text form field.

the template rendering is done as follows
<form method="post">
{% csrf_token %}
{{ form.as_p}}
<button class="btn btn-dark ml-2" type="submit">Save</button>
</form>

in the application admin.py I have the following:
class ArticleAdmin(admin.ModelAdmin):
formfield_overrides = {
models.CharField: {'widget': TextInput(attrs={'size': '100'}), },
}
# registered as
admin.site.register(Article, ArticleAdmin)

Sadly it appears to have no effect.
Viewing the rendered source, I do not see a size attribute.
the field is rendered as:
<input type="text" name="title" maxlength="255" required id="id_title">

Have I ommited a step?
thanks
--
Tim Johnson
http://www.tj49.com

Tim Johnson

unread,
May 11, 2019, 7:43:07 PM5/11/19
to Django ML
* Tim Johnson <t...@akwebsoft.com> [190511 11:20]:
Still no luck.
Tried a custom class:

class LongTextinput(TextInput):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
attrs.setdefault('size', 100)
super(LongTextinput, self).__init__(*args, **kwargs)
...
formfield_overrides = {
models.CharField: {'widget': LongTextinput},
}

No luck.
Replace admin.site.register with decorator:
@admin.register(Article)
still no luck.

ArticleAdmin has an additional item, with
full code below
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
inlines = [CommentInline]
formfield_overrides = {
models.CharField: {'widget': LongTextinput},
}

It appears that inlines is being executed.
It says right here:
https://stackoverflow.com/questions/910169/resize-fields-in-django-admin

that it is supposed to be easy, but after hours of tweaking, I'm convinced that
formfield_overrides is being ignored.

Joe Reitman

unread,
May 12, 2019, 2:23:12 PM5/12/19
to Django users
Tim,

Here is an example of a custom form field limiting the input to 100 characters. The model is defined to accept 255 chars. BTW, the text widget 'attrs' sets the HTML form element attributes.

class SearchForm(forms.Form):

 search_for
= forms.CharField(
 label
='',
 label_suffix
='',
 max_length
=100,
 required
=True,
 widget
=forms.TextInput(attrs={'placeholder': 'search', ' autofocus': ''}),
 help_text
='',
 error_messages
={'required': ''},
 
)

Tim Johnson

unread,
May 12, 2019, 3:02:59 PM5/12/19
to django...@googlegroups.com
* Joe Reitman <jreit...@gmail.com> [190512 10:31]:
> Tim,
>
> Here is an example of a custom form field limiting the input to 100
> characters. The model is defined to accept 255 chars. BTW, the text widget
> 'attrs' sets the HTML form element attributes.
>
> class SearchForm(forms.Form):
>
> search_for = forms.CharField(
> label='',
> label_suffix='',
> max_length=100,
> required=True,
> widget=forms.TextInput(attrs={'placeholder': 'search', ' autofocus': ''}),
> help_text='',
> error_messages={'required': ''},
> )
Hi Joe:
Thanks for the reply.
I did try using a custom class in admin.py
class LongTextinput(TextInput):
def __init__(self, *args, **kwargs):
attrs = kwargs.setdefault('attrs', {})
# code below aims to add a size="100" attribute
# to rendered html
attrs.setdefault('size', 100)
super(LongTextinput, self).__init__(*args, **kwargs)
implemented with formfields_ovverides as
in :
formfield_overrides = {
models.CharField: {'widget': LongTextinput},
}
but did not have the desired effect.

the larger question is:
why is formfield_overrides not having the effect I expected?
I.E. size="100" is added to the rendered html.

According to documentation the implementation should be pretty straightforward.

Do you suppose that there is a particular plugin that enables formfield_overrides?

**OR** is {{ form.as_p }} in the template inhibiting the rendering?

My goal was not to change the max_length attribute, but the size attribute in
the rendered HTML code.

For the record, css widens the input field, and that gave me the
input width I sought, but the edification I seek is to understand
how to use formfield_overrides.

cheers

Joe Reitman

unread,
May 12, 2019, 5:27:25 PM5/12/19
to Django users
Tim,

I found this in the Django source code on Github searching for 'formfield_overrides'. It is in a test module testing formfield_overrides functionality. I'm assuming you imported TextInput from forms? This should work to change your admin display. Are trying to change your Admin panel form or a form going out to the user?


class BandAdmin(admin.ModelAdmin):
 formfield_overrides
= {
 
CharField: {'widget': forms.TextInput(attrs={'size': '10'})}
 
}


Regards,
Joe

On Saturday, May 11, 2019 at 2:10:06 PM UTC-5, tim042849 wrote:

Tim Johnson

unread,
May 12, 2019, 6:03:51 PM5/12/19
to django...@googlegroups.com
* Joe Reitman <jreit...@gmail.com> [190512 13:34]:
> Tim,
>
> I found this in the Django source code on Github searching for
> 'formfield_overrides'. It is in a test module testing formfield_overrides
> functionality. I'm assuming you imported TextInput from forms? This should
> work to change your *admin* display. Are trying to change your Admin panel
> form or a form going out to the user?
>
>
> class BandAdmin(admin.ModelAdmin):
> formfield_overrides = {
> CharField: {'widget': forms.TextInput(attrs={'size': '10'})}
> }
I am trying to change a form going to the user.
The form in the admin page __does__ show the size attribute in
the HTML source code for input/text.

So it appears that formfield_overrides only affects the admin
page.

I'm going to call this solved - given that I can "widen" the field
with css.

Thanks again Joe. You've been a great help and I've learned a lot.
cheers

Jim Illback

unread,
May 12, 2019, 6:20:05 PM5/12/19
to Django users
Tim, this won’t help you to use or solve the formfield_overrides issues, but maybe it will be helpful. I’ve not tried this for a CharField, but it works great on a TextField (which has no max_length, so model size isn’t impacted):

1. Install  widget tweaks and add {% load widget_tweaks %} at the top of your HTML page.
2. On your HTML page, instantiate the field using this format: {{ form.notes|attr:"rows:20"|attr:"cols:50" }} - you can use either row/cols or both as in this case, and any char size.


I hope this is useful for you.

Jim Illback


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20190512220300.GF2372%40mail.akwebsoft.com.
For more options, visit https://groups.google.com/d/optout.

Tim Johnson

unread,
May 12, 2019, 7:51:32 PM5/12/19
to django...@googlegroups.com
* Jim Illback <suba...@hotmail.com> [190512 14:22]:
> Tim, this won’t help you to use or solve the formfield_overrides issues, but maybe it will be helpful. I’ve not tried this for a CharField, but it works great on a TextField (which has no max_length, so model size isn’t impacted):
>
> 1. Install widget tweaks and add {% load widget_tweaks %} at the top of your HTML page.
> 2. On your HTML page, instantiate the field using this format: {{ form.notes|attr:"rows:20"|attr:"cols:50" }} - you can use either row/cols or both as in this case, and any char size.
>
> Here’s a great usage example on widget tweaks: https://simpleisbetterthancomplex.com/2015/12/04/package-of-the-week-django-widget-tweaks.html.
>
> I hope this is useful for you.
Thanks Jim. I had seen references to 'tweaks'
and I will give it a try.
Reply all
Reply to author
Forward
0 new messages