[英]How can I serialize a list via Django REST Framwork
I have a function which base on different Pandas processes it result a list ( [6, 6, 6]
). 我有一个函数,该函数基于不同的Pandas进程生成列表( [6, 6, 6]
6,6,6 [6, 6, 6]
)。 My question is how can I serialize it with Django REST Framwork
. 我的问题是如何使用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. 不幸的是,我还处于休息状态,这是我使用API
端点构建Django
的第一时间。
Here is my attempt: 这是我的尝试:
[serializers.py] [serializers.py]
class TestSerializer(serializers.ListField):
child = serializers.IntegerField()
[views.py] [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. 由于没有Model
,因此根本不必使用queryset
属性或ListCreateAPIView
。 You can use APIView
class as 您可以将APIView
类用作
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 注意 :我认为ListSerializer
和ListField
之间没有太大区别,但是无论如何我还是会推荐ListSerializer
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.