0

我正在编写一个视图集,它接收来自用户的字符串值数组并根据发布请求验证表。我不知道如何从用户那里获取 request.POST 中的数组并使用 Postman 中的其余 API。比如:

class newView(viewsets.ModelViewset):
    authentication_classes = (BasicAuthentication)
    serializer_class = NewSerializer

    def create(self,request,*args,**kwargs):
        stringslist = ???
        # request.POST.????
        # Work with stringslist

另外,如何在邮递员中检查这个?

4

1 回答 1

0
class StringListField(serializers.ListField):
    child = serializers.CharField()

class NewSerializer(serializers.Serializer):
    string_list = StringListField()

在序列化程序中定义字符串列表字段并添加字符串列表

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        string_list = serializer.validated_data.get('string_list', [])
        # you can get string list here
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, 
        headers=headers)
于 2019-02-12T11:04:25.253 回答