I have a function which base on different Pandas processes it result a list ([6, 6, 6]
). My question is how can I serialize it with Django REST Framwork
. I tried to implement the documentation bit I just get different errors. Unfortunately I am newby in rest, and this is the fist time when I build Django
with API
endpoint.
Here is my attempt:
[serializers.py]
class TestSerializer(serializers.ListField):
child = serializers.IntegerField()
[views.py]
class TestListCreate(generics.ListCreateAPIView):
queryset = Test().test_list[0] # This results the list --> [6, 6, 6]
serializer_class = TestSerializer
Since there is no Model
, you don't have to use queryset
attribute or ListCreateAPIView
at all. You can use APIView
class as
from rest_framework.views import APIView
from rest_framework.response import Response
class MyTestView(APIView):
def get(self, request, *args, **kwargs):
my_list_data = Test().test_list[0] # get the data
serializer = TestSerializer(my_list_data)
return Response(data=serializer.data)
and change your serializer as,
class TestSerializer(serializers.ListSerializer):
child = serializers.IntegerField()
Note: I don't think there is much difference between ListSerializer
and ListField
, but I would recommend ListSerializer
anyway