Hi,
I want to make my custom filter into drop down filter,
class PersonIdFilter(SimpleListFilter):
title = _('Personid')
parameter_name ='personid'
contlist = list(PersonInfo.objects.all())
def lookups(self, request, model_admin):
lst = []
for id in self.contlist:
lst.append((id.personid,id.personid))
return tuple(lst)
def queryset(self, request, queryset):
if self.value() is None:
return queryset
return queryset.filter(personid__exact=self.value())
class PersonAdmin (admin.ModelAdmin):
list_filter = (PersonIdFilter,)
The above filter is working for me, but I want to convert it into dropdown filter
I tried with below approach
Create a class in filters.py
from django.contrib.admin.filters import AllValuesFieldListFilter
class DropdownFilter(AllValuesFieldListFilter):
template = 'admin/dropdown_filter.html'
Create a template in templates/admin/dropdown_filter.html
Copied from fiencms
{% load i18n %}
<script type="text/javascript">var go_from_select = function(opt) { window.location = window.location.pathname + opt };</script>
<h3>{{ title }}</h3>
<ul class="admin-filter-{{ title|cut:' ' }}">
{% if choices|slice:"4:" %}
<li>
<select style="width: 95%;"
onchange="go_from_select(this.options[this.selectedIndex].value)">
{% for choice in choices %}
<option{% if choice.selected %} selected="selected"{% endif %}
value="{{ choice.query_string|iriencode }}">{{ choice.display }}</option>
{% endfor %}
</select>
</li>
{% else %}
{% for choice in choices %}
<li{% if choice.selected %} class="selected"{% endif %}>
<a href="{{ choice.query_string|iriencode }}">{{ choice.display }}</a></li>
{% endfor %}
{% endif %}
And in my admin.py
from receivedata.filters import DropdownFilter
class PersonAdmin (admin.ModelAdmin):
list_filter = (PersonIdFilter,DropdownFilter)
Error : type object 'PersonIdFilter' has no attribute 'split'
But it did not works for me,please suggest some other way