在 Django Admin 中调整字段大小 [英] Resize fields in Django Admin

查看:42
本文介绍了在 Django Admin 中调整字段大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 在管理中添加或编辑条目时往往会填满水平空间,但在某些情况下,这是真正的空间浪费,例如,编辑 8 个字符宽的日期字段或 CharField 时,也6 或 8 个字符宽,然后编辑框最多为 15 或 20 个字符.

Django tends to fill up horizontal space when adding or editing entries on the admin, but, in some cases, is a real waste of space, when, i.e., editing a date field, 8 characters wide, or a CharField, also 6 or 8 chars wide, and then the edit box goes up to 15 or 20 chars.

如何告诉管理员文本框的宽度或 TextField 编辑框的高度?

How can I tell the admin how wide a textbox should be, or the height of a TextField edit box?

推荐答案

你应该使用 ModelAdmin.formfield_overrides.

这很容易 - 在 admin.py 中,定义:

It is quite easy - in admin.py, define:

from django.forms import TextInput, Textarea
from django.db import models

class YourModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size':'20'})},
        models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},
    }

admin.site.register(YourModel, YourModelAdmin)

这篇关于在 Django Admin 中调整字段大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆