How can I disable the POST and DELETE methods?

2,449 views
Skip to first unread message

Salvatore Iovene

unread,
Sep 3, 2012, 2:00:08 PM9/3/12
to django-res...@googlegroups.com
Hi,
I want a certain ModelResource to be read-only.

I've been able to disable the POST method in the ListView, by using ListModelView instead of ListOrCreateModelView, but I can't disable DELETE when looking at an instance.

I've created my own view like this:

from djangorestframework.views import ListOrCreateModelView, InstanceModelView                            
from djangorestframework.response import Response                                                         
                                                                                                          
from resources import *                                                                                   
                                                                                                          
class UserProfileInstanceView(InstanceModelView):                                                         
  resource = UserProfileResource                                                                          
                                                                                                          
  def delete(self, request, id):                                                                          
    return self.render(Response(405))


That's fine, I get a 405 METHOD NOT ALLOWED if I try to delete an instance, but the API browser view still shows DELETE as an allowed method.

Is there a place I can disable unwanted methods a bit earlier in the code path? I understand that "allowed_methods" has been removed, so what is the correct way to do it?

Thanks!
Salvatore.
Message has been deleted

Tom Christie

unread,
Sep 4, 2012, 6:13:53 AM9/4/12
to django-res...@googlegroups.com
Hi Salvatore,

Take a look at how the InstanceModelView is composed.
If you want an alternative that doesn't include delete, then build a view class that does the same but doesn't include 'DeleteModelMixin'.

Cheers,

  Tom

Salvatore Iovene

unread,
Sep 4, 2012, 6:16:29 AM9/4/12
to django-res...@googlegroups.com
Thanks Tom,
it's exactly what I came up to (see my reply to myself).

Thanks anyway for taking the time to reply!

Salvatore.
--
Salvatore Iovene

Personal website: http://iovene.com/
Founder of AstroBin: http://astrobin.com/

Tom Christie

unread,
Sep 4, 2012, 6:17:19 AM9/4/12
to django-res...@googlegroups.com
it's exactly what I came up to (see my reply to myself).

Yup, just saw that :)
Great stuff.
Reply all
Reply to author
Forward
0 new messages