1
votes

I am using DjangoRestApi and while it works like a charm with queryset (orm-based) views, I am struggling to make views that use different back-end to behave same way orm-based views are. Notably I want to add filters and have them cast and validated automatically.

Pseudo code below:

class NewsFilter(django_filters.FilterSet):
    category = django_filters.NumberFilter(name='category')
    limit = django_filters.NumberFilter(name='limit')
    page = django_filters.NumberFilter(name='page')


class NewsView(generics.APIView):
    filter_class = NewsFilter

    def get(self, request):
        filters = self.filter_class(??)  # not sure, what to put here

        payload = logic.get_business_news(**filters.data)  # same

        return Response(payload, status=status.HTTP_200_OK)

Any hint how to tackle problem will be appreciated.

Ultimate goal is to:

  • user types something into url or sends via POST, django-rest intercepts relevant values, extracts them, casts them into correct type and return as a dictionary
  • filters are displayed as they would if serializer was ORM based
1

1 Answers

1
votes

The function signature to any single filter is like

class MyFilter(django_filters.Filter):
    def filter(self,queryset,value):
        [...]

The function signature to a FilterSet is:

def __init__(self, data=None, queryset=None, prefix=None, strict=None):

So, it looks like you pass in request.GET as data param and then pass in your queryset.