Overriding Size of Django Admin Multi-select Widget

前端 未结 4 903
梦如初夏
梦如初夏 2020-12-28 17:58

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

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 18:39

    If you have a long select that will keep growing, I recommend to use an autocomplete widget.

    Anyway, you could:

    1. Create a ModelForm, for the model in question

    2. Override the default widget, for the field in question,

    3. Set widget's size attribute to your needs

    4. Enable that form in ModelAdmin, for example

      class YourModelForm(forms.ModelForm):
          class Meta:
              model = YourModel
              widgets = {
                  'your_field': forms.SelectMultiple(attrs={'size': 12})
              }
      
      
      class YourModelAdmin(admin.ModelAdmin):
          form = YourModelForm
      

提交回复
热议问题