Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework APIView register route

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?

like image 200
Mirza Delic Avatar asked Nov 06 '15 13:11

Mirza Delic


2 Answers

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 :

like image 190
iankit Avatar answered Nov 15 '22 08:11

iankit


Routers won't work with APIView. They only work with ViewSets and their derivatives.

You likely want:

class PayOrderViewSet(ModelViewSet):
like image 22
Linovia Avatar answered Nov 15 '22 08:11

Linovia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!