# templatetags/my_tags.py
@register.inclusion_tag('tags/letters_filter.html')
def letters_filter(model):
letters = u'0123456789AĄBCĆDEĘFGHIJKLŁMNŃOPÓQRSŚTUVWXYZŹŻ'
ModelClass = get_model('subjects', model) # could make it even more generic here ('subjects' is my app name)
if ModelClass is None:
raise TemplateSyntaxError("Nie ma takiego modelu %s" % model)
letters_list = [letter for letter in letters \
if ModelClass._default_manager.filter(my_nice_field_name__istartswith=letter).count() > 0]
ret_dict = { 'letters_list' : letters_list }
return ret_dict
# template "tags/letters_filter.html"
{% if letters_list %}
{% for letter in letters_list %}
{{letter|upper}}
{% endfor %}
All
{% endif %}
# filters.py
import django_filters
class MyNiceFilterSet(django_filters.FilterSet):
firstletter = django_filters.CharFilter(lookup_type='istartswith', name='my_nice_field_name' )
class Meta:
model = MyNiceModel