Added AllValuesFilterSpec to admin changelist filters, which lets you put any arbitrary field in Admin.list_filter. To determine the list of all available choices, Django does a SELECT DISTINCT. Note this is backwards-incompatible for people who have defined and registered their own FilterSpecs, because each FilterSpec now takes a 'model' parameter.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3136 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2006-06-16 19:42:06 +00:00
parent 72307a6eee
commit d599052a15
2 changed files with 42 additions and 19 deletions

View File

@ -11,18 +11,18 @@ import datetime
class FilterSpec(object): class FilterSpec(object):
filter_specs = [] filter_specs = []
def __init__(self, f, request, params): def __init__(self, f, request, params, model):
self.field = f self.field = f
self.params = params self.params = params
def register(cls, test, factory): def register(cls, test, factory):
cls.filter_specs.append( (test, factory) ) cls.filter_specs.append((test, factory))
register = classmethod(register) register = classmethod(register)
def create(cls, f, request, params): def create(cls, f, request, params, model):
for test, factory in cls.filter_specs: for test, factory in cls.filter_specs:
if test(f): if test(f):
return factory(f, request, params) return factory(f, request, params, model)
create = classmethod(create) create = classmethod(create)
def has_output(self): def has_output(self):
@ -48,8 +48,8 @@ class FilterSpec(object):
return "".join(t) return "".join(t)
class RelatedFilterSpec(FilterSpec): class RelatedFilterSpec(FilterSpec):
def __init__(self, f, request, params): def __init__(self, f, request, params, model):
super(RelatedFilterSpec, self).__init__(f, request, params) super(RelatedFilterSpec, self).__init__(f, request, params, model)
if isinstance(f, models.ManyToManyField): if isinstance(f, models.ManyToManyField):
self.lookup_title = f.rel.to._meta.verbose_name self.lookup_title = f.rel.to._meta.verbose_name
else: else:
@ -71,31 +71,31 @@ class RelatedFilterSpec(FilterSpec):
for val in self.lookup_choices: for val in self.lookup_choices:
pk_val = getattr(val, self.field.rel.to._meta.pk.attname) pk_val = getattr(val, self.field.rel.to._meta.pk.attname)
yield {'selected': self.lookup_val == str(pk_val), yield {'selected': self.lookup_val == str(pk_val),
'query_string': cl.get_query_string( {self.lookup_kwarg: pk_val}), 'query_string': cl.get_query_string({self.lookup_kwarg: pk_val}),
'display': val} 'display': val}
FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec) FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
class ChoicesFilterSpec(FilterSpec): class ChoicesFilterSpec(FilterSpec):
def __init__(self, f, request, params): def __init__(self, f, request, params, model):
super(ChoicesFilterSpec, self).__init__(f, request, params) super(ChoicesFilterSpec, self).__init__(f, request, params, model)
self.lookup_kwarg = '%s__exact' % f.name self.lookup_kwarg = '%s__exact' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val = request.GET.get(self.lookup_kwarg, None)
def choices(self, cl): def choices(self, cl):
yield {'selected': self.lookup_val is None, yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string( {}, [self.lookup_kwarg]), 'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
'display': _('All')} 'display': _('All')}
for k, v in self.field.choices: for k, v in self.field.choices:
yield {'selected': str(k) == self.lookup_val, yield {'selected': str(k) == self.lookup_val,
'query_string': cl.get_query_string( {self.lookup_kwarg: k}), 'query_string': cl.get_query_string({self.lookup_kwarg: k}),
'display': v} 'display': v}
FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec) FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
class DateFieldFilterSpec(FilterSpec): class DateFieldFilterSpec(FilterSpec):
def __init__(self, f, request, params): def __init__(self, f, request, params, model):
super(DateFieldFilterSpec, self).__init__(f, request, params) super(DateFieldFilterSpec, self).__init__(f, request, params, model)
self.field_generic = '%s__' % self.field.name self.field_generic = '%s__' % self.field.name
@ -123,14 +123,14 @@ class DateFieldFilterSpec(FilterSpec):
def choices(self, cl): def choices(self, cl):
for title, param_dict in self.links: for title, param_dict in self.links:
yield {'selected': self.date_params == param_dict, yield {'selected': self.date_params == param_dict,
'query_string': cl.get_query_string( param_dict, self.field_generic), 'query_string': cl.get_query_string(param_dict, self.field_generic),
'display': title} 'display': title}
FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec) FilterSpec.register(lambda f: isinstance(f, models.DateField), DateFieldFilterSpec)
class BooleanFieldFilterSpec(FilterSpec): class BooleanFieldFilterSpec(FilterSpec):
def __init__(self, f, request, params): def __init__(self, f, request, params, model):
super(BooleanFieldFilterSpec, self).__init__(f, request, params) super(BooleanFieldFilterSpec, self).__init__(f, request, params, model)
self.lookup_kwarg = '%s__exact' % f.name self.lookup_kwarg = '%s__exact' % f.name
self.lookup_kwarg2 = '%s__isnull' % f.name self.lookup_kwarg2 = '%s__isnull' % f.name
self.lookup_val = request.GET.get(self.lookup_kwarg, None) self.lookup_val = request.GET.get(self.lookup_kwarg, None)
@ -142,11 +142,34 @@ class BooleanFieldFilterSpec(FilterSpec):
def choices(self, cl): def choices(self, cl):
for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')): for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')):
yield {'selected': self.lookup_val == v and not self.lookup_val2, yield {'selected': self.lookup_val == v and not self.lookup_val2,
'query_string': cl.get_query_string( {self.lookup_kwarg: v}, [self.lookup_kwarg2]), 'query_string': cl.get_query_string({self.lookup_kwarg: v}, [self.lookup_kwarg2]),
'display': k} 'display': k}
if isinstance(self.field, models.NullBooleanField): if isinstance(self.field, models.NullBooleanField):
yield {'selected': self.lookup_val2 == 'True', yield {'selected': self.lookup_val2 == 'True',
'query_string': cl.get_query_string( {self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]), 'query_string': cl.get_query_string({self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
'display': _('Unknown')} 'display': _('Unknown')}
FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec) FilterSpec.register(lambda f: isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField), BooleanFieldFilterSpec)
# This should be registered last, because it's a last resort. For example,
# if a field is eligible to use the BooleanFieldFilterSpec, that'd be much
# more appropriate, and the AllValuesFilterSpec won't get used for it.
class AllValuesFilterSpec(FilterSpec):
def __init__(self, f, request, params, model):
super(AllValuesFilterSpec, self).__init__(f, request, params, model)
self.lookup_val = request.GET.get(f.name, None)
self.lookup_choices = model._meta.admin.manager.distinct().order_by(f.name).values(f.name)
def title(self):
return self.field.verbose_name
def choices(self, cl):
yield {'selected': self.lookup_val is None,
'query_string': cl.get_query_string({}, [self.field.name]),
'display': _('All')}
for val in self.lookup_choices:
val = str(val[self.field.name])
yield {'selected': self.lookup_val == val,
'query_string': cl.get_query_string({self.field.name: val}),
'display': val}
FilterSpec.register(lambda f: True, AllValuesFilterSpec)

View File

@ -574,7 +574,7 @@ class ChangeList(object):
filter_fields = [self.lookup_opts.get_field(field_name) \ filter_fields = [self.lookup_opts.get_field(field_name) \
for field_name in self.lookup_opts.admin.list_filter] for field_name in self.lookup_opts.admin.list_filter]
for f in filter_fields: for f in filter_fields:
spec = FilterSpec.create(f, request, self.params) spec = FilterSpec.create(f, request, self.params, self.model)
if spec and spec.has_output(): if spec and spec.has_output():
filter_specs.append(spec) filter_specs.append(spec)
return filter_specs, bool(filter_specs) return filter_specs, bool(filter_specs)