0

I am using django-rest-framework's genericAPIViews

I want to send some data from the front end to the backend and depending upon the data sent Django should query a model and return some data to the frontend. The data sent is protected data and thus can't be attached in the URL so, GET request can't be used. I am not manipulating the database, just querying it and returning a response (a typical GET use case).

Now in DRF's genericAPIViews, I can't find a view which does this:

As can be seen from Tom Christie's GitHub page only 2 views have a post handler:

  1. CreateAPIView: return self.create()
  2. ListCreateAPIView: return self.create()

As can be seen both these views have post methods which create entries in the database which I don't want. Is there a built-in class which does my job or should I use generics.GenericAPIView and write my own post handler?

Currently I am using generic.View which has post(self, request, *args, **kwargs)

coda
  • 2,188
  • 2
  • 22
  • 26

1 Answers1

0

I think you have a few options to choose from. One way is to use a ModelViewSet which could be quite useful because of how it nicely handles the communication between views, serializers and models. Here is a link to django-rest-framework ModelViewSet docs.

These are the actions that it provides by default (since it inherits from GenericAPIView):

.list(), .retrieve(), .create(), .update(), .partial_update(), .destroy().

If you don't want all of them you could specify which methods you want by doing the following:

class ModelViewSet(views.ModelViewSet):
    queryset = App.objects.all()
    serializer_class = AppSerializer
    http_method_names = ['get', 'post', 'head']

Note: http_method_names seems to be working from Django >= 1.8

Source: Disable a method in a ViewSet, django-rest-framework

Community
  • 1
  • 1
Sync
  • 3
  • 5