我想通过序列化程序对用户输入进行验证,但在验证之后我希望得到带有json类型(str,int,bool,float而不是python实例)的dict,而没有可由用户传递的多余字段。 例如:
serializer = SomeSerializer(data=request.data)
# request.data = {'a':'abc', 'some_primary_key':33, 'c':'abc'}
if serializer.is_valid():
# serializer has fields 'a' and 'some_primary_key' and doesn't have 'c'
serializer.validated_data == {'a': 'abc', 'some_primary_key': <python instance with pk=33>}
如何在验证后得到类似serializer.plain_validated_data == {'a': 'abc', 'some_primary_key':33}
的内容?
答案 0 :(得分:0)
An easy way to get it would be to serialize the deserialization's result:
serializer = SomeSerializer(data=request.data)
# request.data = {'a':'abc', 'some_primary_key':33, 'c':'abc'}
if serializer.is_valid():
# serializer has fields 'a' and 'some_primary_key' and doesn't have 'c'
output_serializer = SomeSerializer(instance=serializer.validated_data)
output_serializer.data == {'a': 'abc', 'some_primary_key':33}