简体   繁体   English

可以访问序列化程序验证的数据DRF

[英]get access to serializer validated data DRF

all the django rest framework docs assume your going to instantly save the data. 所有的django rest框架文档都假设您将立即保存数据。 But what if I want access to the serializer data? 但是,如果我想访问序列化数据呢? What if I want to do something with it. 如果我想用它做什么怎么办? Or if the serializer contains info other than what I need to save in a model? 或者,如果序列化程序包含的信息不是我需要保存在模型中的信息?

is the validated_data attribute what we need? validated_data属性我们需要什么?

So 所以

validatedData = serializer.validated_data
userid = validatedData.get('id')

would work right? 会工作吗?

Yes you are right, but remember the validated_data is generate only after you call .is_valid() method. 是的,你是对的,但请记住,只有在调用.is_valid()方法后才会生成validated_data For example: 例如:

serializer.is_valid()
# do it first ^^^^^
validatedData = serializer.validated_data
userid = validatedData.get('id')

You can look on example saving-instances in the docs 您可以在文档中查看示例保存实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM