RestFul API endpoints with Django Rest Framework Class based views












0














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 ?










share|improve this question


















  • 2




    But that is exactly what viewsets are for. What "other restrictions"?
    – Daniel Roseman
    Nov 20 '18 at 11:08










  • @Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
    – Amistad
    Nov 20 '18 at 11:49










  • You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
    – Daniel Roseman
    Nov 20 '18 at 11:53
















0














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 ?










share|improve this question


















  • 2




    But that is exactly what viewsets are for. What "other restrictions"?
    – Daniel Roseman
    Nov 20 '18 at 11:08










  • @Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
    – Amistad
    Nov 20 '18 at 11:49










  • You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
    – Daniel Roseman
    Nov 20 '18 at 11:53














0












0








0







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 ?










share|improve this question













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 ?







python django django-rest-framework django-urls django-rest-viewsets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:43









Amistad

2,57062438




2,57062438








  • 2




    But that is exactly what viewsets are for. What "other restrictions"?
    – Daniel Roseman
    Nov 20 '18 at 11:08










  • @Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
    – Amistad
    Nov 20 '18 at 11:49










  • You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
    – Daniel Roseman
    Nov 20 '18 at 11:53














  • 2




    But that is exactly what viewsets are for. What "other restrictions"?
    – Daniel Roseman
    Nov 20 '18 at 11:08










  • @Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
    – Amistad
    Nov 20 '18 at 11:49










  • You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
    – Daniel Roseman
    Nov 20 '18 at 11:53








2




2




But that is exactly what viewsets are for. What "other restrictions"?
– Daniel Roseman
Nov 20 '18 at 11:08




But that is exactly what viewsets are for. What "other restrictions"?
– Daniel Roseman
Nov 20 '18 at 11:08












@Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
– Amistad
Nov 20 '18 at 11:49




@Daniel Roseman..how do I use different permission classes in view sets for different methods like list, create etc ?
– Amistad
Nov 20 '18 at 11:49












You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
– Daniel Roseman
Nov 20 '18 at 11:53




You can create a custom permissions class, or you can just define those methods in your subclass and check permissions before calling the super method.
– Daniel Roseman
Nov 20 '18 at 11:53












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391242%2frestful-api-endpoints-with-django-rest-framework-class-based-views%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391242%2frestful-api-endpoints-with-django-rest-framework-class-based-views%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy