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
}
}

相关文章:

  • 2021-07-04
  • 2021-05-24
  • 2022-01-04
  • 2022-01-20
  • 2021-04-26
  • 2021-06-19
猜你喜欢
  • 2021-09-04
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2021-12-26
相关资源
相似解决方案