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
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:
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))
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>
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
http://djangosuit.com/ also offers dropdowns for list filters.
I cannot comment answers so I'll add to beholderrk's answer here.
dropdown_filter.html
or similardropdown_filter.html
create a new filter class in filters.py
:
from django.contrib.admin.filters import AllValuesFieldListFilter
class DropdownFilter(AllValuesFieldListFilter):
template = 'admin/dropdown_filter.html'
now you can use this filter in your admin class:
class SomeAdmin(admin.ModelAdmin):
# ...
list_filter = (('country', DropdownFilter),)
Works great!
An easy option would be to use django-grappelli, which replaces all the filters with drop downs.
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',)