Monday, October 20, 2014

dropdown_filter in django

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bb9893cb-8927-4b33-886b-da4c4d9c8ce6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment