0

I'm using a Generic CreateAPIView to save a model in the database. Here's my code:

class AppointmentCreateAPIView(generics.CreateAPIView):
    permission_classes = (AppointmentCreatePermission,)
    queryset = Appointment.objects.all()
    serializer_class = AppointmentSerializer

And in my urls.py file, I have this:

urlpatterns[
    url(r'^appointments/create', AppointmentCreateAPIView.as_view()),
]

This url obviously supports the POST operation. However, I want to use this same url to handle a GET request, which would fetch the data necessary to populate the appointment creation form. I understand that I can use separate urls for get and post, but that's not what I'm looking for. Is it possible that I keep the same url, but with different HTTP Verb, the view would be able to handle both GET and POST request?

Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63
QuestionEverything
  • 4,809
  • 7
  • 42
  • 61

1 Answers1

1

You can do this by manually adding get method to your view, it would look something like this. Code below probably will not work, but will give you general idea.

from rest_framework.response import Response

class AppointmentCreateAPIView(generics.CreateAPIView):
    permission_classes = (AppointmentCreatePermission,)
    queryset = Appointment.objects.all()
    serializer_class = AppointmentSerializer

    def get(self, request, *args, **kwargs):
        serializer = AppointmentSerializer({your_data})
        return Response(serializer.data)
Sardorbek Imomaliev
  • 14,861
  • 2
  • 51
  • 63