Django处理枚举-枚举模型
- 1、定义模型类、序列化器类
- 2、对上面这些场景使用source参数
- 3、支持连表查询
- 4、自定义序列化输出方法
- 5、案例5
1、定义模型类、序列化器类
定义模型类models.py;项目模型类、接口模型类、用例模型类
from django.db import modelsclass Test_Project(models.Model):name = models.CharField(help_text='项目名称', verbose_name='项目名称',max_length=60,unique=True)desc = models.TextField(help_text='项目描述', blank=True, null=True)create_time = models.DateTimeField(auto_now_add=True)update_time = models.DateTimeField(auto_now=True)class Meta:db_table = "t_test_project"verbose_name = "项目表"verbose_name_plural = verbose_nameordering = ['id']def __str__(self):return self.name
CHOICES = [('1', '项目接口'),('2', '外部接口')]
type = models.CharField(verbose_name='接口类型', help_text='接口类型', max_length=40,default='1', choices=CHOICES)
from django.db import modelsclass Test_Interface(models.Model):CHOICES = [('1', '项目接口'),('2', '外部接口')]name = models.CharField(help_text='接口名称', verbose_name='接口名称',max_length=60,unique=True)desc = models.TextField(help_text='接口描述', blank=True, null=True, default='描述')type = models.CharField(verbose_name='接口类型', help_text='接口类型',max_length=40,default='1', choices=CHOICES)project = models.ForeignKey(Test_Project, on_delete=models.CASCADE)playcount=models.IntegerField(help_text='访问量',verbose_name='访问量',default=0)commentcount = models.IntegerField(default=0, verbose_name='评论量')class Meta:db_table = 't_test_interface'verbose_name = "接口表"verbose_name_plural = verbose_nameordering = ['id']def __str__(self):return self.name
from django.db import modelsclass Test_Testcase(models.Model):name = models.CharField(help_text='用例名称', verbose_name='用例名称',max_length=60,unique=True)desc = models.TextField(help_text='用例名称', blank=True, null=True)create_time = models.DateTimeField(auto_now_add=True)update_time = models.DateTimeField(auto_now=True)interface = models.ForeignKey(Test_Interface, on_delete=models.CASCADE)class Meta:db_table = "t_test_testcase"verbose_name = "用例表"verbose_name_plural = verbose_nameordering = ['id']def __str__(self):return self.name
序列化器类设计
from rest_framework import serializersfrom ttest.models import Test_Project,Test_Interfaceclass TProjectSerializer(serializers.ModelSerializer):class Meta:model=Test_Projectfields='__all__'class TInterfaceSerializer(serializers.ModelSerializer):class Meta:model=Test_Interfacefields='__all__'
访问接口:前端展示的数据type:“1”
使用默认的序列化器时,视图函数访问 具有choices参数 的字段或 一对一 或 一对多 或 多对多 字段时,返回的数据只有 id 值,就像下面这种方式,性别是0或1
2、对上面这些场景使用source参数
get_xxx_display 用于显示 choices 参数对应的文本信息。
序列化器文件:serializers.py
class TProjectSerializer(serializers.ModelSerializer):class Meta:model=Test_Projectfields='__all__'class TInterfaceSerializer(serializers.ModelSerializer):type = serializers.ChoiceField(choices=Test_Interface.CHOICES,source="get_type_display",read_only=True) # 设置source="get_属性_display"即可class Meta:model=Test_Interfacefields='__all__'
3、支持连表查询
project_create_time=serializers.CharField(source='project.create_time')
显示 一对一 或 一对多 或 多对多 字段对应的文本信息。支持连表查询
from rest_framework import serializers
from ttest.models import Test_Project,Test_Interfaceclass TProjectSerializer(serializers.ModelSerializer):class Meta:model=Test_Projectfields='__all__'class TInterfaceSerializer(serializers.ModelSerializer):type = serializers.ChoiceField(choices=Test_Interface.CHOICES,source="get_type_display",read_only=True) # 设置source="get_属性_display"即可project_create_time=serializers.CharField(source='project.create_time')class Meta:model=Test_Interfacefields='__all__'
4、自定义序列化输出方法
自定义序列化输出方法,会寻找并执行’get_xxx’的方法。
from rest_framework import serializers
from ttest.models import Test_Project,Test_Interfaceclass TProjectSerializer(serializers.ModelSerializer):class Meta:model=Test_Projectfields='__all__'class TInterfaceSerializer(serializers.ModelSerializer):type = serializers.ChoiceField(choices=Test_Interface.CHOICES,source="get_type_display",read_only=True) # 设置source="get_属性_display"即可project_create_time=serializers.CharField(source='project.create_time')name_zidingyi=serializers.SerializerMethodField()def get_name_zidingyi(self,obj):return obj.project.nameclass Meta:model=Test_Interfacefields='__all__'
5、案例5
序列化输出时改变前端输出的字段名称