Manually Operating Viewsets in Django Rest Framework to Retrieve Serialized Data

Django
2020-05-19 15:34 (4 years ago) ytyng
from rest_framework.request import Request as DRFRequest

class HomeDataView(APIView):

    def get(self, request, *args, **kwargs):
        data = _inner_view(request)
        ...

    def _inner_view(drf_request):
        dr = DRFRequest(drf_request._request)
        dr.query_params._mutable = True
        dr.query_params['page_size'] = 5
        dr.query_params['intellectualproperty'] = ip_id
        vs = ProductViewSet(request=dr, action='list')
        vs.initial(dr)
        return vs.list(ds).data


    # If you want to create and process a QuerySet yourself
    def _inner_view():
        queryset = Schedule.objects.filter(...)
        paginator = ScheduleViewSet.pagination_class()
        page = paginator.paginate_queryset(queryset, self.request)
        serializer = ScheduleViewSet.serializer_class(page, many=True)
        response = paginator.get_paginated_response(serializer.data)
        return response.data
Currently unrated

Comments

Archive

2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011