I am currently using an APIView for a url ('/api/events/<int:pk>'), but I can't use this same APIView for the list view as there is no distinction between list (get all) and detail (get 1). This view is unrelated to a model, because I am calling an externel API.
The ViewSet does have distinction between list and retrieve, but if I use that, I HAVE to use the serializer_classes. But, I don't have a serializer on this view.
I know I can create a second APIView so I have 1 for both the detail and the list view, but I find that a little cumbersome. It's cleaner to have it in 1 class.
What to do best?
Example:"
class TestView(APIView):
"""
Test123123o8
"""
serializer_class = Test1Serializer
#this methods acts on the /test GET route
def get(self, request):
return Response(Test.objects.all())
# route that acts on the DETAIL route (for example '/api/test/1').
# How to implement this in the URL?
def get_detail(self, request, pk):
return Response(test.objects.get(pk=pk))
def post(self, request, format=None):
serializer = Test1Serializer(data=request.data)
if serializer.is_valid():
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)