How to programmatically call a Django Rest Framework view within another view?

I found the solution for this in the documentation... https://docs.djangoproject.com/en/1.7/topics/class-based-views/mixins/

Hint is from their example here:

class AuthorDetail(View):

    def get(self, request, *args, **kwargs):
        view = AuthorDisplay.as_view()
        return view(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        view = AuthorInterest.as_view()
        return view(request, *args, **kwargs)

As of Django 2.2 and DRF 3.9.2 I am able to get response using below code.

response = UserItemsApiView.as_view()(request=request._request).data

Above example solves below issues:

  • The request argument must be an instance of django.http.HttpRequest, not rest_framework.request.Request
  • Instead of content, using data attribute gave me result from that view.

html_from_view = ExampleDetail.as_view({'get': 'list'})(request).content

OR

html_from_view = ExampleDetail.as_view({'get': 'retrieve'})(request, pk=my_id).render().content