implement custom filter

This commit is contained in:
Sanj 2011-08-25 16:55:52 +05:30
parent 4a5e61e2b0
commit 9fae2d95ca

View File

@ -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