Created
April 21, 2020 22:24
-
-
Save icarovirtual/0af4d0c6a46beb30240880345255fa5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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