Django rest framework APIView register route

I cannot register APIView for my URL routes.

Code from views:

 class PayOrderViewSet(APIView): queryset = PayOrder.objects.all() 

Code from URL:

 router = routers.DefaultRouter() router.register(r'document/payorder', PayOrderViewSet) 

This newly created URL does not exist at all.

What is the solution for this?

+5
source share
2 answers

Routers will not work with APIView . They work only with ViewSets and their derivatives.

You probably want:

 class PayOrderViewSet(ModelViewSet): 
+3
source

Routers and APIViews (general or others) are two different ways to create API endpoints. Routers only work with views.

In your code, you are trying to create a view for a router in which your code extends the APIView class.

Your problem will take care of what @linovia has proposed in its Anyer. I would suggest that it would be nice to understand the difference between the two.

GenericViewSet inherits from GenericAPIView, but does not provide any implementation of the main actions. Only get_object, get_queryset.

ModelViewSet inherits from GenericAPIView and includes implementations for various actions. In other words, you do not need to implement basic actions in the form of a list, retrieval, creation, update or destruction. Of course, you can override them and implement your own list or your own creation methods.

More about viewsets and a universal class based on APIViews :

+5
source

Source: https://habr.com/ru/post/1235366/


All Articles