from django.contrib import admin
from .filters import DropdownFilter
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from .models import User
def 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_choice
class 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)
The code is all about creating custom text input filters to the django admin site, here I want to pass the every field name to the title and I added the queryset function to the dynamically created class but seems like it's not working and I got an error like below
NotImplementedError at /admin/epic/user/
subclasses of ListFilter must provide a queryset() method
How to solve this problem?
你应该用大括号调用函数,比如 queryset()。
list_filter = [type('UserFilter',(InputFilter,),{'title' : _(i),'parameter_name' : i, 'queryset': queryset}) for i in list_display]
它现在正在工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.