Skip to content

Instantly share code, notes, and snippets.

@icarovirtual
Created April 21, 2020 22:24
Show Gist options
  • Save icarovirtual/0af4d0c6a46beb30240880345255fa5b to your computer and use it in GitHub Desktop.
Save icarovirtual/0af4d0c6a46beb30240880345255fa5b to your computer and use it in GitHub Desktop.
class BaseFilterView:
# List of admin filters
filters = [StatusFilter]
def get_form_kwargs(self):
form_kwargs = super().get_form_kwargs()
# Pass the filters to the form
form_kwargs['filters'] = self.filters
return form_kwargs
def apply_filters(self, queryset):
# Map filters submitted in the request to their values
filter_params = dict(self.request.GET.items())
for filter_cls in self.filters:
# Apply the filter if any parameter match the filter
# `parameter_name`
filter_obj = filter_cls(self.request, filter_params, None, None)
queryset = filter_obj.queryset(self.request, queryset)
return queryset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment