#5833: Custom FilterSpecs
-------------------------------------------+--------------------------------
Reporter: Honza_Kral | Owner: jkocherhans
Status: assigned | Milestone:
Component: django.contrib.admin | Version: SVN
Resolution: | Keywords: nfa-someday
list_filter filterspec nfa-changelist ep2008
Stage: Accepted | Has_patch: 1
Needs_docs: 1 | Needs_tests: 1
Needs_better_patch: 0 |
-------------------------------------------+--------------------------------
Comment (by bendavis78):
It seems that the latest patch did not support using a "field-less"
filterspec. The validation was failing because it was still expecting a
field. I've attached a patch against 1.2.1 that works for me. If anyone
involved could please test this out, that would be great.
We need some docs and tests for this -- it'd be nice to see this added to
1.3. Although, it would be good to get some feedback from the core devs
to make sure this is going in the right direction.
In the meantime, here's a silly little example of how a custom non-field-
specific filterspec can be used (in case anyone wants to play around with
it).
{{{
#!python
from testapp import models
from django.contrib.admin.filterspecs import FilterSpec
from django.contrib import admin
from django.utils.datastructures import SortedDict
class HowYummyFilterSpec(FilterSpec):
def __init__(self, request, params, model, model_admin):
super(HowYummyFilterSpec, self).__init__(request, params, model,
model_admin)
self.links = SortedDict((
('All', 'all'),
('Double Rainbow', 'so_intense'),
('Pretty good', 'good'),
('Not great', 'not_great'),
('Gross', 'gross'),
))
def consumed_params(self):
return self.links.values()
def choices(self, cl):
selected = [v for v in self.links.values() if
self.params.has_key(v)]
for title, key in self.links.items():
yield {'selected': self.params.has_key(key),
'query_string': cl.get_query_string({key:1}, selected),
'display': title}
def get_query_set(self, cls, qs):
if self.params.has_key('so_intense'):
return qs.filter(name__icontains='bacon')
if self.params.has_key('good'):
return qs.filter(name__icontains='burger')
if self.params.has_key('not_great'):
return qs.filter(name__icontains='haggas')
if self.params.has_key('gross'):
return qs.filter(name__icontains='guano')
return qs
def title(self):
return u'How yummy?'
class FoodAdmin(admin.ModelAdmin):
list_filter = [HowYummyFilterSpec]
admin.site.register(models.Food, FoodAdmin)
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/5833#comment:68>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.