提醒:只是笔记,不是教程,仅仅供本人后期回顾用。
serializer = SnippetSerializer(data=data) serializer.is_valid() # True serializer.validated_data serializer.save() # <Snippet: Snippet object>
- 通过data初始化一个serializer实例后,在访问serializer.validated_data和serializer.data之前必须调用is_valid()函数;在访问了serializer.data后无法调用serializer.save(),如下代码错误:
serializer = SnippetSerializer(data=data) serializer.is_valid() # True serializer.data serializer.save() # AssertionError: You cannot call `.save()` after accessing # `serializer.data`.If you need to access data before committing #to the database then inspect 'serializer.validated_data' instead.
网友评论