I have the following 4 class based views in DRF to perform CRUD operation on a model called Trips.
from rest_framework import generics
class TripCreateView(CreateAPIView):
#code that creates a Trip
class TripListView(ListAPIView):
#code that lists Trips
class TripDetailView(RetrieveAPIView):
#code that gives details of a Trip
class TripUpdateView(UpdateAPIView):
#code that updates a particular trip details
class TripDeleteView(DestroyAPIView):
#code that deletes an instance
Now in-order to wire up the urls to each view, my urls.py looks like this:
urlpatterns = [
url(r'^trip/$', TripCreateView.as_view()),
url(r'^trip/list/$',TripListView.as_view()),
url(r'^trip/(?P<pk>[0-9]+)/detail/$', TripDetailView.as_view()),
url(r'^trip/(?P<pk>[0-9]+)/update/$', TripUpdateView.as_view()),
url(r'^trip/(?P<pk>[0-9]+)/delete/$', TripDeleteView.as_view())
]
This works as expected.However, as is evident, those API endpoints are poorly designed since the URI has the http method as well in it. RESTFUL API endpoints don't have the HTTP method in the URI as look like this:
Endpoint HTTP METHOD Result
trips GET Gets all Trips
trips/:id GET Gets details of a Trip
trips POST Creates a Trip
trips/:id PUT Updates a Trip
trips:/id DELETE Deletes a Trip
I know Viewsets can help achieve this but I can't use them because of certain other restrictions.Can this be achieved just by using class based views that I am using ?