我的admin.py
文件中有以下过滤器:
class parentCategoryFilter(admin.SimpleListFilter):
title = 'parent category'
parameter_name = 'parent_category'
def lookups(self, request, model_admin):
first_level_categories = model_admin.get_queryset(request).filter(parent_category__isnull=True)
if first_level_categories:
lookups = (('none', 'None'),)
for first_level_category in first_level_categories:
lookups += ((first_level_category.id, first_level_category.name),)
return lookups
def queryset(self, request, queryset):
if self.value() == 'none':
return queryset.filter(parent_category__isnull=True)
elif self.value():
try:
return queryset.filter(parent_category=int(self.value()))
except (ValueError, TypeError):
return queryset.none()
else:
pass
我想在我的tests.py
文件中对其进行测试,但是在尝试实例化该类时,它开始询问我大约 5 个__init__
参数。是否可以测试此过滤器功能?