3

I have been coding using django for the past year or so and most of my work was writing API's to connect to the React-based frontend. In most of my tutorials I see people using GenericAPIViews for the basics, but I don't find anything substantial for complicated code in APIs. So to deliver my code in time, I chose the easier solution that is to use APIView where I use less abstraction, have more control over my code (by writing more code) and understand clearly the functions of my code modules.

What I'm concerned is that if I've chosen a shortcut by choosing not to learn how to use GenericAPIViews so that I can use it at its full potential. Some of the problems I faced while using GenericAPIViews or DRF in general.

  • Custom permissions (e.g. I want a user who is authenticated and also have access with specific permission level (e.g can_do_xyz, or has_access_to_abc)
  • Using writable serializers which might need to go through complex layers of business/app logic
  • Creating entries for multiple models in the same GenericAPIView class function.

For now, the problems I have faced in my approach to use basic APIView is that the swagger documentation I've managed to write is garbage.

  • I need to manually specify the query/form/path fields in the api schema docs (using AutoSchema)
  • Also if an API has get/post/put/delete methods with varying fields, each of the API endpoints in swagger will show ALL the fields being used across the methods (which is understandable since I define the schema for the class and not for the separate functions). Is there any way to solve this problem?
  • The delete method should only need the survey_id as parameter, while the POST/PUT method would need the body.
Mehran
  • 1,264
  • 10
  • 27

2 Answers2

2

Personaly I prefer use GenericAPIViews and the suitables mixins, is more verbose but I have more control over the REST action added, and I think it help to others developer that work with my projects.

For manage the rest documentation, you can use drf-yasg that allow tell the serializer for each method.

josemlp
  • 496
  • 3
  • 5
  • 1
    Thanks for letting me know about drf-yasg! I'll have to try it out. Also, if you don't mind do you have any tutorial/blog that writes in detail about how to use mixins in DRF (or python in general)? – Mehran Mar 11 '19 at 05:09
0

i will not suggest the GenericAPIView because, suppose you have an API for get method only and you are using GenericAPIView, it will unnecessary show lots of methods(get/put/delete/post) whereas you need only get. so that case i ll suggest you to use anyone among UpdateAPIView, RetrieveAPIView, ListAPIView, CreateAPIView according to your need

Ankush Sahu
  • 578
  • 7
  • 13
  • What do you mean by that ? if you don't specify for example `def post(self,request): your code` trying to access `put` will return `"detail": "Method \"POST\" not allowed."`, you can choose to use only what you need. Sometimes you just need or want to handle things manually thus the usage of `APIView` or `GenericAPIView` instead of using more magic `restframework.generics` classes. – Heroe__ Jan 27 '22 at 18:52