Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-rest-framework limit the allowed_methods to GET

I Have just started with django-rest-framework. Pretty enthousiastic about it, except for the fact there are very little examples available. getting the api working is going great, but all the extra's is a puzzle. (adding extra custom fields etc.)

Now I wonder how you can restrict the allowed_methods in for example a ListView or a DetailView. Adding this to the class in the views.py like I read somewhere as an answer... does not seem to have any effect:

allowed_methods = ('GET',)
like image 364
michel.iamit Avatar asked Mar 03 '12 21:03

michel.iamit


People also ask

Why is Django REST so slow?

The Django REST Framework(DRF) is a framework for quickly building robust REST API's. However when fetching models with nested relationships we run into performance issues. DRF becomes slow. This isn't due to DRF itself, but rather due to the n+1 problem.

What is difference between Django and Django REST Framework?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.

Is Django REST Framework necessary?

Django REST Framework is only necessary if you're building a RESTful API; An HTTP service that reads and writes data, usually as JSON payloads. Services are typically created to allow external clients such as mobile apps, single page applications (React, Angular, etc.) or 3rd parties to gain access to your data.


1 Answers

If you are using ModelViewSet and still want to restrict some methods you can add http_method_names.

Example:

class SomeModelViewSet(viewsets.ModelViewSet):
    queryset = SomeModel.objects.all()
    serializer_class = SomeModelSerializer
    http_method_names = ['get', 'post', 'head']

Once you do this, get, post and head will be allowed. But put, patch and delete will not be allowed.

like image 51
Akshar Raaj Avatar answered Nov 01 '22 06:11

Akshar Raaj