标签:不包含 方法 bsp ade project 序列 connect sel 包含
在没有使用ModelSerializer序列化器类之前,我们定义的序列化器类都需要添加对应模型类字段的很多字段,如果添加的字段特别多,那么势必会影响开发效率和代码的可读性,因此会选择一种更为简洁的序列化器类来帮我们优化框架代码,简化序列化器类中字段的创建
常规序列化器类示例:
from rest_framework import serializers from rest_framework import validators from .models import Projects class ProjectSerializer(serializers.Serializer): name = serializers.CharField(max_length=200, label="项目名称", help_text=‘项目名称‘, validators=[validators.UniqueValidator(queryset=Projects.objects.all(), message="项目名字段name必须唯一"), name_is_not_contain_x],) leader = serializers.CharField(max_length=50, label="项目负责人", help_text=‘项目负责人‘) programmer = serializers.CharField(max_length=50, label="开发人员", help_text="开发人员") tester = serializers.CharField(max_length=50, label="测试人员", help_text="测试人员")
使用ModelSerializer如下,已经简化了很多代码:
from rest_framework import serializers from .models import Projects class ProjectsModelSerializer(serializers.ModelSerializer): class Meta: model = Projects fields = ‘__all__‘
规则总结如下:
直接使用ModelSerializer实例化的对象替换掉原先的序列化器类对象即可
from django.http import JsonResponse from django.views import View from django.db import connection import json from .models import Projects from .serializers import ProjectsModelSerializer class ProjectsPage(View): ‘‘‘ 类视图 ‘‘‘ def post(self, request): input_data = json.loads(request.body) serializer_check_obj = ProjectsModelSerializer(data=input_data) if not serializer_check_obj.is_valid(): return JsonResponse({"code": 1, "res": "error", "msg": serializer_check_obj.errors}) serializer_check_obj.save() return JsonResponse(serializer_check_obj.validated_data, status=201)
在Meta子类中定义类属性fields=‘__all__‘
fields = ‘__all__‘
在Meta子类中定义类属性fields=(字段1,字段2,......)
fields = (‘name‘, ‘leader‘, ‘programmer‘)
在Meta子类中定义类属性exclude=(字段1,字段2,......)
exclude = (‘desc‘, )
在Meta子类中定义类属性read_only_fields=(字段1,字段2,......)
read_only_fields = (‘update_time‘, )
其优先级最高,如果以该类方式创建的方式很多,则应该选择普通的序列化器类来定义
from rest_framework import serializers from rest_framework import validators from .models import Projects class ProjectsModelSerializer(serializers.ModelSerializer): name = serializers.CharField(max_length=200, label="项目名称", help_text=‘项目名称‘, validators=[validators.UniqueValidator(queryset=Projects.objects.all(), message="项目名字段name必须唯一")], ) class Meta: model = Projects fields = ‘__all__‘
键为字段名,值为一个字典,其中键为校验项,值为校验规则
from rest_framework import serializers from rest_framework import validators from .models import Projects class ProjectsModelSerializer(serializers.ModelSerializer): class Meta: model = Projects fields = ‘__all__‘ extra_kwargs = { ‘name‘: { ‘max_length‘: 50, ‘validators‘: [validators.UniqueValidator(queryset=Projects.objects.all(), message="项目名字段name必须唯一")] }, ‘tester‘: { ‘max_length‘: 200 } }
from rest_framework import serializers from rest_framework import validators from .models import Projects def name_is_not_contain_x(value): if ‘X‘ in value.upper(): raise serializers.ValidationError("项目名字段name不能包含x的大小写字符") class ProjectsModelSerializer(serializers.ModelSerializer): email = serializers.EmailField(read_only=True) class Meta: model = Projects fields = (‘id‘, ‘name‘, ‘leader‘, ‘programmer‘, ‘tester‘, ‘create_time‘, ‘update_time‘, ‘email‘) extra_kwargs = { ‘name‘: { ‘max_length‘: 50, ‘validators‘: [validators.UniqueValidator(queryset=Projects.objects.all(), message="项目名字段name必须唯一"), name_is_not_contain_x] }, ‘tester‘: { ‘max_length‘: 200 } } def validate_name(self, value): if ‘项目‘ in value: raise serializers.ValidationError("项目名称name字段不能包含‘项目’字符") return value def validate(self, attrs): if ‘A‘ in attrs.get(‘name‘) and ‘B‘ in attrs.get(‘leader‘): raise serializers.ValidationError("项目名称字段name不包含A的同时项目负责人字段leader也不能包含B") return attrs def create(self, validated_data): validated_data.pop(‘email‘) return super().create(validated_data)
DRF框架serializer之ModelSerializer
标签:不包含 方法 bsp ade project 序列 connect sel 包含
原文地址:https://www.cnblogs.com/xiaogongjin/p/13287826.html