I am not able to register an APIView to my url routes.
Code from views :
class PayOrderViewSet(APIView):
queryset = PayOrder.objects.all()
Code from urls :
router = routers.DefaultRouter()
router.register(r'document/payorder', PayOrderViewSet)
This newly created url doesn't exist at all.
What is solution for this?
Routers and APIViews (generic or otherwise) are two different ways to create API endpoints. Routers work with viewsets only.
In your code, you are although trying to create a viewset for a router your code is extending APIView class.
Your problem will be taken care by what @linovia has suggested in his asnwer. I would suggest it will be good idea to understand the difference between those two.
GenericViewSet inherits from GenericAPIView but does not provide any implementations of basic actions. Just only get_object, get_queryset.
ModelViewSet inherits from GenericAPIView and includes implementations for various actions. In other words you dont need implement basic actions as list, retrieve, create, update or destroy. Of course you can override them and implement your own list or your own create methods.
Read More about viewsets and Generic Class Based APIViews :
Routers won't work with APIView. They only work with ViewSets and their derivatives.
You likely want:
class PayOrderViewSet(ModelViewSet):
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With