覆盖 Django Admin 多选小部件的大小 [英] Overriding Size of Django Admin Multi-select Widget

查看:25
本文介绍了覆盖 Django Admin 多选小部件的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手 Django 问题:我希望 Django 管理员在多选小部件中显示更多行选择.我有一个非常长的列表可供选择,默认的 4 行并不方便,尤其是在滚动时.

Newbie Django question: I'd like the Django admin to display more rows of choices in the multi-select widget. I have an extremely long list to select from and the default 4 rows just isn't convenient, especially when scrolling.

现在,该小部件呈现为 select multiple,但我希望它是 select multiple size="12".理想情况下,这应该针对特定字段,但我可以忍受所有字段都使用相同的大小属性呈现.

Right now, that widget is rendered as select multiple but I'd like it to be select multiple size="12". Ideally this should be for specific fields, but I can live with all fields rendering with the same size attribute.

那么,在 Django 中,哪里是改变这一点的最佳位置呢?

So where would be the optimal place in Django to change that?

推荐答案

我在 admin.py 中通过为所有 ManyToManyField 项设置一个总括大小来做到这一点,例如:

I did this in the admin.py by setting a blanket size for all ManyToManyField items, for instance:

from django.contrib import admin
from django.forms import SelectMultiple
from django.db import models
from models import *

class RiverAdmin(admin.ModelAdmin):
    formfield_overrides = { models.ManyToManyField: {'widget': SelectMultiple(attrs={'size':'10'})}, }

admin.site.register(River, RiverAdmin)

这篇关于覆盖 Django Admin 多选小部件的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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