model = 需要用到的序列化模型类
fields = (‘需要序列化的字段‘)
如果多个视图需要数据大致相同可以通过
extra_kwargs = {‘声明那些字段参与序列化,那些不参与序列化‘}
例子:
class UserDetailSerialzier(serializers.ModelSerializer):
class Meta:
model = User
fields = (‘id‘, ‘username‘, ‘mobile‘, ‘email‘, ‘email_active‘)
extra_kwargs = {
‘username‘: {
‘read_only‘: True
},
‘mobile‘: {
‘read_only‘: True
},
‘email_active‘: {
‘read_only‘: True
}
}
原文:https://www.cnblogs.com/wangdongpython/p/10834495.html