from django.contrib import adminfrom .filters import DropdownFilterfrom django.db.models import Qfrom django.utils.translation import gettext_lazy as _from .models import Userdef queryset(self, request, queryset): term = self.value() if term is None: return term = term.lower username = Q() for bit in term.spilt(): username = Q(username, queryset) return queryset.filter(username)class InputFilter(admin.filters.SimpleListFilter): template = 'admin/input_filter.html' def lookups(self, request, model_admin): # Dummy, required to show the filter. return ((),) def choices(self, changelist): # Grab only the "all" option. all_choice = next(super().choices(changelist)) all_choice['query_parts'] = ((k, v)for k, v in changelist.get_filters_params().items() if k != self.parameter_name) yield all_choiceclass TransactionAdmin(admin.ModelAdmin): list_display = [field.name for field in User._meta.fields] c = type('UserFilter',(InputFilter,),{'title' : _('title'),'parameter_name' : 'search', 'query': queryset}) c.queryset = queryset list_filter = [type('UserFilter',(InputFilter,),{'title' : _(i),'parameter_name' : i, 'query': queryset}) for i in list_display]admin.site.register(User, TransactionAdmin)代码是关于创建自定义文本输入过滤器到django管理站点,在这里我想将每个字段名称传递给标题,并将查询集函数添加到动态创建的类中,但似乎它不起作用,我得到了如下错误 NotImplementedError at /admin/epic/user/ subclasses of ListFilter must provide a queryset() method如何解决这个问题?
添加回答
举报
0/150
提交
取消