标签:com serialize employees 通过 exclude 校验器 lock 短信验证码 默认
ModelSerializer
ModelSerializer与常规的Serializer相同,但提供了:
- 基于模型类自动生成一系列字段
- 基于模型类自动为 Serializer 生成 validators,比如字段唯一的校验器
- 包含默认的 create() 和 update() 的实现
比如我们创建一个 DepartmentSerializer2
class DepartmentSerializer2(serializers.ModelSerializer): class Meta: model = Department fields = ‘__all__‘ # 包含模型类中所有的字段
在 python console
交互环境中,查看生成的序列化器
>>> s = DepartmentSerializer2() >>> s DepartmentSerializer2(): id = IntegerField(label=‘ID‘, read_only=True) name = CharField(label=‘部门名称‘, max_length=20) create_date = DateField(label=‘成立时间‘) is_delete = BooleanField(label=‘是否删除‘, required=False)
1) fields
属性:指定序列化器中包含哪些字段,如下:
class DepartmentSerializer2(serializers.ModelSerializer): class Meta: model = Department fields = (‘id‘, ‘name‘)
2) exclude
属性:排除掉模型类中指定的字段
class DepartmentSerializer2(serializers.ModelSerializer): class Meta: model = Department exclude = (‘is_delete‘,) # 注意:`exclude`不能与`fields`同时使用
3) 指明只读字段
可以通过 read_only_fields
指明只读字段,即 仅用于序列化输出,在反序列化时不会进行校验和修改
class DepartmentSerializer2(serializers.ModelSerializer): class Meta: model = Department fields = ‘__all__‘ read_only_fields = (‘id‘, ‘create_date‘) # 这些字段不能修改
4) 指定 关联属性的序列化方式
class EmployeeSerializer2(serializers.ModelSerializer): # 1. 返回关联对象的主键 department = PrimaryKeyRelatedField(read_only=True) # 2. 返回关联对象的字符串表示(即Department类的 __str__ 方法的返回值) # department = StringRelatedField(read_only=True) # 3. 返回关联对象序列化器定义的属性 # department = DepartmentSerializer(read_only=True) class Meta: model = Employee fields = ‘__all__‘ class DepartmentSerializer2(serializers.ModelSerializer): # 名字固定: 类名小写_set employee_set = PrimaryKeyRelatedField(read_only=True, many=True) # employee_set = StringRelatedField(read_only=True, many=True) # employee_set = EmployeeSerializer(read_only=True, many=True) class Meta: model = Department fields = ‘__all__‘ # ok # fields = (‘id‘, ‘name‘, ‘employee_set‘) # ok # fields = (‘id‘, ‘name‘) # error
extra_kwargs
属性我们可以使用extra_kwargs参数为ModelSerializer添加或修改原有的选项参数
class EmployeeSerializer2(serializers.ModelSerializer): class Meta: model = Employee fields = ‘__all__‘ extra_kwargs = { ‘name‘: {‘min_length‘: 5, ‘max_length‘: 20}, ‘age‘: {‘min_value‘: 1, ‘max_value‘: 200}, } # 结果如下: EmployeeSerializer2(): id = IntegerField(label=‘ID‘, read_only=True) name = CharField(label=‘姓名‘, max_length=20, min_length=5) age = IntegerField(label=‘年龄‘, max_value=200, min_value=1) gender = ChoiceField(choices=((0, ‘男‘), (1, ‘女‘)), label=‘性别‘, required=False, validators=[<django.core.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>]) salary = DecimalField(decimal_places=2, label=‘工资‘, max_digits=8) comment = CharField(allow_blank=True, allow_null=True, label=‘备注‘, max_length=300, required=False) hire_date = DateField(label=‘入职时间‘, read_only=True) department = PrimaryKeyRelatedField(label=‘所属部门‘, queryset=Department.objects.all())
标签:com serialize employees 通过 exclude 校验器 lock 短信验证码 默认
原文地址:https://www.cnblogs.com/chichung/p/9938813.html