Skip to content

Instantly share code, notes, and snippets.

@bukowa
Last active February 14, 2023 02:59
Show Gist options
  • Save bukowa/e0db31680846e0d0e3e7c55140c757a7 to your computer and use it in GitHub Desktop.
Save bukowa/e0db31680846e0d0e3e7c55140c757a7 to your computer and use it in GitHub Desktop.
django filter mixin
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