我有一个如下所示的类,后来使用序列化程序来获取python数据类型:
from datetime import datetime
from rest_framework import serializers
class Comment(object):
def __init__(self, email, content, created=None):
self.email = email
self.content = content
self.created = created or datetime.now()
并创建了一个对象:
comment = Comment(email='leila@example.com', content='foo bar')
serializer = serializers.Serializer(comment).data
返回空的ReturnDict:
ReturnDict()
,而:
如果我定义了一个扩展serializers.Serializer的类
class CommentSerializer(serializers.Serializer):
pass
然后
serializer = CommentSerializer(comment).data
ReturnDict([('email', 'leila@example.com'),
('content', 'foo bar'),
('created', '2018-04-01T03:33:58.627773Z')])
为什么serializers.Serializer只在扩展时才起作用