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

后端 未结 9 852
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  • 2020-12-13 04:16

    You can copy the admin templates from the django installation into you templates/admin folder in your project.

    Then you will need to do any of 2 things in the forms or templates you want to show your outputs in:

    1. If you are working with a form, in that you would like the list choices to be posted back to a database, you would in your model.py, on the field you have your choices, put in some this like this:

      choice = forms.IntegerField(widget=forms.Select(choices=CHOICES))
      
    2. If it is just to display on a page, then you will output on a template tag something like this:

      <select>
        {% for choices in object.details.all %}
          <option> {{ object.choice }} </option>
        {% endfor %}
      </select>
      
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-13 04:27

    http://djangosuit.com/ also offers dropdowns for list filters.

    0 讨论(0)
  • 2020-12-13 04:29

    I cannot comment answers so I'll add to beholderrk's answer here.

    1. create a new template called dropdown_filter.html or similar
    2. copy the code of filter.html from feincms to dropdown_filter.html
    3. create a new filter class in filters.py:

      from django.contrib.admin.filters import AllValuesFieldListFilter
      
      class DropdownFilter(AllValuesFieldListFilter):
          template = 'admin/dropdown_filter.html'
      
    4. now you can use this filter in your admin class:

      class SomeAdmin(admin.ModelAdmin):
          # ...
          list_filter = (('country', DropdownFilter),)
      

    Works great!

    0 讨论(0)
  • 2020-12-13 04:34

    An easy option would be to use django-grappelli, which replaces all the filters with drop downs.

    0 讨论(0)
  • 2020-12-13 04:36

    I am not a fan of all solutions provided up to now.

    Why? If, for a field that you want to filter by, you have more than 10 values, a listview box isn't that handy, too. I advice to use the standard search field capability of django admin which will show you a search field:

    class BooksAdmin(admin.ModelAdmin):
        list_display = ('ISBN', 'title')         
        search_fields = ('ISBN',)
        # instead of: list_filter = ('ISBN',)
        ordering = ('title',)  
    
    0 讨论(0)
提交回复
热议问题