serializers.SerializerMethodField和钩子方法结合,可以实现对ModelSerializer类的一些字段进行二次加工,返回,如下:
1、对以ModelSerializer的类为外键数据进行处理
model如下:
A、Project
class Project(models.Model):
"""
项目表
"""
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, verbose_name='项目名称')
version = models.CharField(max_length=50, verbose_name='版本')
description = models.CharField(max_length=1024, blank=True, null=True, verbose_name='描述')
status = models.BooleanField(default=True, verbose_name='状态')
LastUpdateTime = models.DateTimeField(auto_now=True, verbose_name='最近修改时间')
createTime = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, max_length=1024, verbose_name='创建人')
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Meta:
verbose_name = '项目'
verbose_name_plural = '项目'
B、ApiInfo (以project为外键)
class ApiInfo(models.Model):
"""
接口信息
"""
id = models.AutoField(primary_key=True)
project = models.ForeignKey(Project, related_name='api_project', on_delete=models.CASCADE, verbose_name='所属项目')
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Meta:
verbose_name = '接口'
verbose_name_plural = '接口管理'
2、project的ModelSerializer如下:
class ProjectSerializer(serializers.ModelSerializer):
"""
项目信息序列化
"""
apiCount = serializers.SerializerMethodField()
LastUpdateTime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
createTime = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
user = serializers.CharField(source='user.first_name')
class Meta:
model = Project
fields = ('id', 'name', 'version', 'type', 'status', 'LastUpdateTime', 'createTime', 'apiCount',
'dynamicCount', 'memberCount', 'description', 'user')
def get_apiCount(self, obj):
return obj.api_project.all().count()
通过 obj.以它为外键的类字段的related_name 取出以它为外键的对象,该对象可以进行进行一般的model类操作(如obj.api_project.all().count()、obj.api_project.get(XXX查询条件).XXX属性),从而处理以它为外键的数据
2、对ModelSerializer的类里的choice等复杂字段的处理
model如下,有choice复杂字段status
class ApiInfo(models.Model):
"""
接口信息
"""
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, verbose_name='接口名称')
status = models.CharField(max_length=50, verbose_name='状态', choices=API_STATUS_CHOICES)
ModelSerializer如下:
class ApiDownLoanListSerializer(serializers.ModelSerializer):
"""
下载接口列表信息序列化
"""
status = serializers.SerializerMethodField()
def get_status(self, obj):
return obj.get_status_display()
class Meta:
model = ApiInfo
fields = ('id', 'name','status')
通过serializers.SerializerMethodField 和钩子方法结合get_status_display()得到choice类数据
3、对ModelSerializer的类里的onetone类型数据取值
model如下,AutomationCaseApi
class AutomationCaseApi(models.Model):
"""
用例执行接口
"""
id = models.AutoField(primary_key=True)
automationTestCase = models.ForeignKey(AutomationTestCase, on_delete=models.CASCADE,
verbose_name='用例', related_name="api")
name = models.CharField(max_length=50, verbose_name='接口名称')
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Meta:
verbose_name = '用例接口'
verbose_name_plural = '用例接口管理'
AutomationHeadRaw,和AutomationCaseApi一对一
class AutomationHeadRaw(models.Model):
"""
测试用例的请求的json形式参数
"""
id = models.AutoField(primary_key=True)
automationCaseApi = models.OneToOneField(AutomationCaseApi, related_name='headerRaw',
on_delete=models.CASCADE, verbose_name='接口')
data = models.TextField(verbose_name='源数据请求头json数据', blank=True, null=True)
class Meta:
verbose_name = '请求头json格式参数'
verbose_name_plural = '请求头json格式参数管理'
ModelSerializer如下:
class AutomationCaseApiParamSerializer(serializers.ModelSerializer):
"""
自动化用例接口入参信息序列化
"""
headerRaw = serializers.SerializerMethodField()
def get_headerRaw(self, obj):
if obj.headerRaw:
return obj.headerRaw.data
return None
class Meta:
model = AutomationCaseApi
fields = ('id', 'name', 'apiAddress', 'parameterRaw', 'responseParameterRaw', 'headerRaw')
通过serializers.SerializerMethodField 和钩子方法结合obj.related_name得到choice类数据