Manually Operating Viewsets in Django Rest Framework to Retrieve Serialized Data
2020-05-19 06:34 (6 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
Related posts
To Create a Request in Django Unit Tests, Retrieving the wsgi_request from the Response is Convenient
Paging Django Model Instances Queried with Complex Filters Using Paginator
Executing Management Commands from Within a View Function
Trying Out Django TemplateView with Extensive Use of @cached_property
Please rate this article (No signup or login required)
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.