2
votes

Ok, I have a GenericAPIView that is supposed to generate a form in the Browsable API cause it declares a post method:

from rest_framework import status
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response

from transacciones.serializers import BillSerializer


class ProcessBill(GenericAPIView):

    serializer_class = BillSerializer

    def post(self, request):

        recieved_data = request.data

        print(recieved_data)

        return Response("Processed Bill", status=status.HTTP_200_OK)

But it doesn't generate a form. The view looks like this (it is in spanish, I translated the code to english so it is be more confortable to read):

enter image description here

I understand I get a 405 Method not Allowed cause I don't define a get method, only a post.

Im using Django 1.8, Django REST Framework 3.3.1 and python 3.4. I'm lost here. Any ideas?

EDIT

Also, if I use Postman (Chrome add on) the view responds correctly.

4
@Geotob This question uses a GenericAPIView, the old one uses a APIView. The difference is stated in the answer to this question by TomChristie : stackoverflow.com/questions/14616489/… - Alejandro Veintimilla
Anyway I thought It might be a good idea to create a new question, maybe it wasn't a good idea. I could delete one of them. - Alejandro Veintimilla
Sorry I didn't spot this! :( GenericAPIViews use a create(request, *args, **kwargs) method, not post - Geotob
@Geotob tried the create method, doesn't work. Anyway, GenericAPIView extends from the APIView, that what the docs say ... so it should have a post method. - Alejandro Veintimilla

4 Answers

2
votes

If you are using generics.RetrieveUpdateDestroyAPIView then you will have to define permission_classes which can be custom.

class ObjectDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    Object RUD (Read Update Delete) API
    """
    permission_classes = ( checkUserPermission, )
    queryset = Object.objects.all()
    serializer_class = ObjectSerializer

OR If you want to just test this, use permission_classes = ( AllowAny, )

class ObjectDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    Object RUD (Read Update Delete) API
    """
    permission_classes = ( AllowAny, )
    queryset = Object.objects.all()
    serializer_class = ObjectSerializer
1
votes

You can use this

from rest_framework.generics import CreateAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.response import Response

from transacciones.serializers import BillSerializer


class ProcessBill(CreateModelMixin, GenericAPIView):
    serializer_class = BillSerializer

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def create(self, request, *args, **kwargs):
        # Do whatever you want here

        # Then invoke the create method and create your instance
        return super().create(request, *args, **kwargs)

Or as an alternative, you just inherit from CreateAPIView which does essentially the same as the code above :)

0
votes

I had the same issue, with postman I saw it was a credential issue, so changing current user status to staff solved the problem.

0
votes

I ran into the same problem. In my case, the mistake was in the serializer, where I used django.db.models.CharField(...) instead of rest_framework.serializers.CharField(...).