* Tim Johnson <[email protected]> [190511 11:20]:
> 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">
> 
  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.

-- 
Tim Johnson
http://www.tj49.com

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/20190511234232.GD2372%40mail.akwebsoft.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to