我使用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)
我的views.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": ""
}
有什么方法吗?还是什么?
答案 0 :(得分:0)
尝试一下:
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)
您需要相应地更改代码-在序列化器/反序列化位置。编码愉快。