这是一个已知的bug:
https://code.djangoproject.com/ticket/15221
这是一种解决方法(1.4版本)。这是丑陋的,但至少它不是猴子修补:
from django.contrib.admin import RelatedFieldListFilter
from django.contrib.admin.util import get_model_from_relation
class WorkAroundListFilter(RelatedFieldListFilter):
def __init__(self, field, request, params, model, model_admin, field_path):
super(RelatedFieldListFilter, self).__init__(
field, request, params, model, model_admin, field_path)
other_model = get_model_from_relation(field)
self.lookup_title = field.verbose_name
rel_name = other_model._meta.pk.name
self.lookup_kwarg = '%s__%s__exact' % (self.field_path, rel_name)
self.lookup_kwarg_isnull = '%s__isnull' % (self.field_path)
self.lookup_val = request.GET.get(self.lookup_kwarg, None)
self.lookup_val_isnull = request.GET.get(
self.lookup_kwarg_isnull, None)
self.lookup_choices = field.get_choices(include_blank=False)
self.title = self.lookup_title
class SomeModelAdmin(admin.ModelAdmin):
list_filter = (
('deposit', WorkAroundListFilter),
('withdrawal', WorkAroundListFilter),
)
你使用的是什么版本的Django?看来SimpleListFilter方法的子类只存在于开发版本中,而不是1.3 –
1.4。我认为这可能是一个错误。不需要子类化任何Filter类。我已经添加了一个错误报告。让我们看看发生了什么。 –
是的,这是一个错误。 –