Я пытался применить "SimpleListFilter" к классу "InputFilter", но я получал HTML содержимого в фильтрах? - PullRequest
0 голосов
/ 30 января 2020
class InputFilter(admin.SimpleListFilter):
    template = 'nyc-theme/lms/templates/admin/course_enrollment/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 SupervisorNameFilter(InputFilter):
    title = 'Supervisor Name'
    parameter_name = 'supervisor'

    enter image description here def lookups(self, request, model_admin):
         kwargs = {
            'profile__supervisor_name__isnull': False
         }
         if 'profile__agency__id__exact' in request.GET:
             agency_id = request.GET['profile__agency__id__exact']
             kwargs['profile__agency__id__exact'] = agency_id
         if 'division' in request.GET:
             division_id = request.GET['division']
             kwargs['profile__division__id__exact'] = division_id
        users = model_admin.model.objects.filter(**kwargs).distinct()
         return [(user.profile.supervisor_ernumber, user.profile.supervisor_name) for user in users]

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(profile__supervisor_name__icontains=self.value())

class SupervisorERNFilter(InputFilter):
    title = 'Supervisor ERN'
    parameter_name = 'ern'

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(profile__supervisor_ernumber__icontains=self.value())

...