這是一個已知的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類。我已經添加了一個錯誤報告。讓我們看看發生了什麼。 –
是的,這是一個錯誤。 –