Overriding Size of Django Admin Multi-select Widget

前端 未结 4 889
梦如初夏
梦如初夏 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:32

    I was able to make number of rows according to initial number of related table rows, however it does not updates dynamically (probably need to insert Javascript into admin form and query number of rows via AJAX, that would be too big to post here).

    class ProfileAdminForm(forms.ModelForm):
        class Meta:
            model = Profile
            fields = '__all__'
            widgets = {
                # Will dynamically change number of rows in select multiple, however only after server reload.
                'spec_profiles': forms.SelectMultiple(attrs={'size': SpecProfile.objects.count()})
            }
    
    class ProfileAdmin(admin.ModelAdmin):
        form = ProfileAdminForm
    

提交回复
热议问题