# Authors: Marinho Brandao <marinho at gmail.com>
# Guilherme M. Gondim (semente) <semente at taurinus.org>
# File: <your project>/admin/filterspecs.py
from django.db import models
from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext as _
class AlphabeticFilterSpec(ChoicesFilterSpec):
"""
Adds filtering by first char (alphabetic style) of values in the admin
filter sidebar. Set the alphabetic filter in the model field attribute
'alphabetic_filter'.
my_model_field.alphabetic_filter = True
"""
def __init__(self, f, request, params, model, model_admin):
super(AlphabeticFilterSpec, self).__init__(f, request, params, model,
model_admin)
self.lookup_kwarg = '%s__istartswith' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
values_list = model.objects.values_list(f.name, flat=True)
# getting the first char of values
self.lookup_choices = list(set(val[0] for val in values_list if val))
self.lookup_choices.sort()
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')}
for val in self.lookup_choices:
yield {'selected': smart_unicode(val) == self.lookup_val,
'query_string': cl.get_query_string({self.lookup_kwarg: val}),
'display': val.upper()}
def title(self):
return _('%(field_name)s that starts with') % \
{'field_name': self.field.verbose_name}
# registering the filter
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'alphabetic_filter', False),
AlphabeticFilterSpec))
Comments
super!
#
Line 55 is better changed like this:
self.lookup_choices = list(set(val[0] for val in values_list if val))
BY adding 'if val' it prevents the routine to crash when indexing items that are blank!
#
snippet updated.
magicrebirth, thanks for the tip.
#
Request Method: GET Request URL: http://vi/voip/phonebook/ Exception Type: TypeError Exception Value:
'long' object is unsubscriptable
#
Thanks! This is very helpful.
#
Very helpful. I've made the following changes. I hope it helps
Avoid duplicates for lower and uppercase
The register must be called explicitely
#
Need to add field_path to init:
#
Solution to "'long' object is unsubscriptable"
Change
26 self.lookup_choices = list(set(val[0] for val in values_list if val))
to
26 self.lookup_choices = list(set(str(val)[0] for val in values_list if val))
This issue rises at ForeignKeys. The solution returns the first character of the id's rather than an error message.
#