Manually Operating Viewsets in Django Rest Framework to Retrieve Serialized Data
Django
2020-05-19 06:34 (5 years ago)

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
Please rate this article
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.