1

With function-based views you can restrict a view to some HTTP methods like so:

@api_view(["GET", "POST"])
def view(request):
    ...

which is neat and explicit. Is there a native way to do the same for a generic view without resorting to if self.request.method == ... conditions?

2
  • Many of the generic views will have this impliciltly builf in. ListViews should be GET only. Update views should accets POST's. Delete should accept deletes... Commented Sep 6, 2015 at 20:04
  • I just wanted to disallow OPTIONS for ListCreateAPIView Commented Sep 6, 2015 at 20:14

2 Answers 2

3

One just needs to set http_method_names attribute on the generic view's class:

class MyListCreateView(generics.ListCreateAPIView):
    http_method_names = ["get", "post"]
    ...

Curiously enough, http_method_names is not documented in relation to class-based or generic views either in Class Based Views nor Generic Views sections of the API Guide.

Sign up to request clarification or add additional context in comments.

1 Comment

But all views are Django based views so documentation docs.djangoproject.com/en/1.8/ref/class-based-views/base/…
2

Yes, simply add: http_method_names like in standard Django View:

Example:

class MyView(ListAPIView):
    http_method_names = ['get', 'post']
    # ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.