gpt4 book ai didi

python - Django Rest Framework 列表字段

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:18 25 4
gpt4 key购买 nike

我使用 Django Rest 框架,我对嵌套序列化程序有一些疑问

我的序列化器

class SerUserSubCreateView(serializers.Serializer):
email = serializers.EmailField()
is_active = serializers.BooleanField()

class SerUserCreateView(serializers.Serializer):
user = SerUserSubCreateView()
clients_id = serializers.CharField(max_length=50)

我的观点.py

class CreateUserView(CreateAPIView):
serializer_class = serializers.SerUserCreateView
permission_classes = [IsAuthenticated]
queryset = UserClients.objects.all()


def post(self,request,format=None):
serializer = self.serializer_class(data=request.data)

if serializer.is_valid():
.................

我的背景

{
"user": {
"email": "",
"status": false
},
"clients_id": ""
}

但我想要

{
"user":[
{
"email": "",
"status": false
},
],
"clients_id": ""
}

有什么方法吗?或者什么?

最佳答案

试试这个:

class SerUserCreateView(serializers.Serializer):  
user = serializers.ListSerializer(child=SerUserSubCreateView())
clients_id = serializers.CharField(max_length=50)

最终:

class SerUserCreateView(serializers.Serializer):  
user = SerUserSubCreateView(many=True)
clients_id = serializers.CharField(max_length=50)

您需要相应地更改代码 - 在序列化器/反序列化位置。快乐编码。

关于python - Django Rest Framework 列表字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53276030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com