Manually Operating Viewsets in Django Rest Framework to Retrieve Serialized Data
Django
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
Please rate this article (No signup or login required)
Currently unrated
Related posts
How to Fix Display Issues in Django Admin Inlines After Upgrading to 5.1
Return Login and Redirect Views by Hardcoding Social Account Provider in django-allauth (When Using AWS Cognito, etc.)
After Upgrading to Django 5 and Updating Dependencies, SSL: CERTIFICATE_VERIFY_FAILED Error Occurs When Sending Emails
The "first" and "second" Arguments of assertEqual in Django's Unit Tests are Actually "expected" and "actual"
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.