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.
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.
So where would be the optimal place in Django to change that?
You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form.
attrs . A dictionary containing HTML attributes to be set on the rendered DateInput and TimeInput widgets, respectively. If these attributes aren't set, Widget. attrs is used instead.
The widget in Django will handle the rendering of the HTML elements and extract data from a POST/GET dictionary that corresponds to the same widget. Whenever we specify a field on a Django form, Django uses some default widget appropriate to the data type to be displayed.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With