Last active
February 14, 2023 02:59
-
-
Save bukowa/e0db31680846e0d0e3e7c55140c757a7 to your computer and use it in GitHub Desktop.
django filter mixin
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 FilterMixin: | |
filter_model = None | |
filter_params = {} | |
def get_filter_params(self): | |
filter_params = {} | |
for k, v in self.filter_params.items(): | |
param = self.request.GET.get(k) | |
if param: | |
filter_params[v] = param | |
return filter_params | |
def get_queryset(self): | |
filter_params = self.get_filter_params() | |
return self.filter_model.objects.filter(**filter_params) | |
class ScriptViewSet(FilterMixin, ModelViewSet): | |
serializer_class = ScriptSerializer | |
filter_model = Script | |
filter_params = { | |
'name': 'name__iexact', | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment