From 9fae2d95caa54991f720cae12e03bfb46e869251 Mon Sep 17 00:00:00 2001 From: Sanj Date: Thu, 25 Aug 2011 16:55:52 +0530 Subject: [PATCH] implement custom filter --- gazetteer/places/admin.py | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/gazetteer/places/admin.py b/gazetteer/places/admin.py index 26334ab..a4159c4 100644 --- a/gazetteer/places/admin.py +++ b/gazetteer/places/admin.py @@ -2,6 +2,49 @@ from django.contrib.gis import admin from models import * from django import forms + +class FeatureTypeFilter(admin.SimpleListFilter): + # Human-readable title which will be displayed in the + # right admin sidebar just above the filter options. + title = _('Feature Type') + + # Parameter for the filter that will be used in the URL query. + parameter_name = 'feature_type' + + def lookups(self, request, model_admin): + """ + Returns a list of tuples. The first element in each + tuple is the coded value for the option that will + appear in the URL query. The second element is the + human-readable name for the option that will appear + in the right sidebar. + """ + fts = tuple() + for ft in FeatureType.objects.all(): + if ft.feature_set.count() > 0: + t = (ft.id, ft.__unicode__(),) + fts.append(t) + + return ( + fts, + ) + + def queryset(self, request, queryset): + """ + Returns the filtered queryset based on the value + provided in the query string and retrievable via + `self.value()`. + """ + # Compare the requested value (either '80s' or 'other') + # to decide how to filter the queryset. + val = self.value() + return queryset.filter(feature_type=val) + + +class PersonAdmin(ModelAdmin): + list_filter = (DecadeBornListFilter,) + + class FeatureNamesInline(admin.StackedInline): model = Name extra = 0 @@ -28,6 +71,7 @@ class FeatureAdmin(admin.OSMGeoAdmin): inlines = [FeatureNamesInline] list_display = ('__unicode__', 'feature_type_name', 'time_start', 'time_end',) list_per_page = 12 + list_filter = ('feature_type__feature_class',) openlayers_url = 'http://openlayers.org/dev/OpenLayers.js' openlayers_img_path = None form = featuresForm