How to change the Django admin filter to use a dropdown instead of list?

后端 未结 9 860
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 03:58

If, for a field that you want to filter by, you have more than ~10 values, the filtering sidebar starts to be ugly and harder to use.

I\'m looking for a solution to

9条回答
  •  Happy的楠姐
    2020-12-13 04:25

    Could you please give a complete example. it shows like before. here is my code

    from django.contrib import admin
    from pages.models import Post, Device, DeviceType, DeviceModel, Ipaddress, DeviceGroup, Location,Department,Comment
    from django_admin_listfilter_dropdown.filters import DropdownFilter, RelatedDropdownFilter
    
    
    class CommentInline(admin.TabularInline):
        model = Comment
    
    class IpaddressAdmin(admin.ModelAdmin):
            prepopulated_fields = {'slug': ('ipaddress',)}
    #        model=Ipaddress
    
            search_fields = ['ipaddress', ]
    #     
            list_display = ('ipaddress', 'machinename', 'user', 'department','location',)
            list_filter = (
            ('user', DropdownFilter),
            ('department', RelatedDropdownFilter),
            ('location', RelatedDropdownFilter),
    
        )
    

    Here is the screenshot

提交回复
热议问题