vendredi 17 juin 2016

Django REST Framework - can't override list in ListAPIView

I am using Django REST Framework to create an endpoint that will produce a PDF document. The PDF document will have information that corresponds to a particular Department. I have two desired functionalities -- to be able to download a PDF document, and to be able to preview the document within the browser.

Since the PDF document changes over time based on data that is added to the app, the document needs to be generated in real time when it is requested. As a first step, I'm trying to have the document be generated in a remote file storage location when the following endpoint is hit by a GET request:

departments/<department_pk>/result/preview

Since my endpoint should only take GET requests, I am using a ListAPIView. I'm trying to override the list method so that my custom document generation logic is executed, but it looks like the method is never called. How can I have some custom document generation logic be inserted into my endpoint, so that it is executed when the endpoint is hit by a GET request?

api/urls.py

url(r'^departments/(?P<department_pk>[0-9]+)/result/preview',
    include(result_document_urls.result_document_preview_router.urls,

document_app/urls.py

result_document_preview_router = routers.DefaultRouter()

result_document_preview_router.register(r'^', ResultDocumentDetailView.as_view(),
    base_name='Department')

document_app/views.py

class ResultDocumentDetailView(generics.ListAPIView):

    queryset = Department.objects.all()
    lookup_field = 'department_pk'
    lookup_url_kwarg = 'department_pk'

    def list(self, request, department_pk):
        queryset = self.get_queryset()
        import ipdb; ipdb.set_trace() # this break point is never hit
        department = get_object_or_404(queryset, department_pk=department_pk)
        ...generate document logic...
        return Response(status=status.HTTP_200_OK)

Aucun commentaire:

Enregistrer un commentaire