CSDN话题挑战赛第2期 参赛话题:学习笔记
案例:以项目表、接口表、用例表、配置表为例
模型类
项目模型类
class Projects(BaseModel):
name = models.CharField('项目名称', max_length=200, unique=True, help_text='项目名称')
leader = models.CharField('负责人', max_length=50, help_text='项目负责人')
tester = models.CharField('测试人员', max_length=50, help_text='项目测试人员')
programmer = models.CharField('开发人员', max_length=50, help_text='开发人员')
publish_app = models.CharField('发布应用', max_length=100, help_text='发布应用')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, default='', help_text='简要描述')
class Meta:
db_table = 'tb_projects'
verbose_name = '项目信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
接口模型类
class Interfaces(BaseModel):
name = models.CharField('接口名称', max_length=200, unique=True, help_text='接口名称')
project = models.ForeignKey('projects.Projects', on_delete=models.CASCADE,
related_name='interfaces', help_text='所属项目')
tester = models.CharField('测试人员', max_length=50, help_text='测试人员')
desc = models.CharField('简要描述', max_length=200, null=True, blank=True, help_text='简要描述')
class Meta:
db_table = 'tb_interfaces'
verbose_name = '接口信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
用例模型类
class Testcases(BaseModel):
name = models.CharField('用例名称', max_length=50, unique=True, help_text='用例名称')
interface = models.ForeignKey('interfaces.Interfaces', on_delete=models.CASCADE,
help_text='所属接口')
include = models.TextField('前置', null=True, help_text='用例执行前置顺序')
author = models.CharField('编写人员', max_length=50, help_text='编写人员')
request = models.TextField('请求信息', help_text='请求信息')
class Meta:
db_table = 'tb_testcases'
verbose_name = '用例信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
配置模型类
class Configures(BaseModel):
name = models.CharField('配置名称', max_length=50, help_text='配置名称')
interface = models.ForeignKey('interfaces.Interfaces',
on_delete=models.CASCADE,
related_name='configures',
help_text='所属接口')
author = models.CharField('编写人员', max_length=50, help_text='编写人员')
request = models.TextField('请求信息', help_text='请求信息')
class Meta:
db_table = 'tb_configures'
verbose_name = '配置信息'
verbose_name_plural = verbose_name
ordering = ('id',)
def __str__(self):
return self.name
需求1:获取接口的列表数据时,需要输出项目名称;创建接口时需要传递项目的id
🔥🔥🔥特别注意1:
当使用PrimaryKeyRelatedField进行反序列输入操作时,要指定查询集,目的是进行校验
🔥🔥🔥特别注意2:
序列化和反序列化操作时,当从表获取父表的数据,序列化字段定义只能用2种方法project和project_id: interfaces_obj.project_id interfaces_obj.project.id
🔥🔥🔥特别注意3
PrimaryKeyRelatedField:在进行反序列输入时,传递的参数是id,但是在序列化输入完成后转化为模型对象了,所以在创建数据时需要处理下,
怎么处理? 1.可以在调用create()方法前对validated_data进行处理, 2.或者在对字段校验前调用to_internal_value(data)方法处理,在字段进行校验前最开始执行的方法
序列化器类设计
class InterfaceSerializer(serializers.ModelSerializer):
project=serializers.StringRelatedField(read_only=True,label='项目名称',help_text='项目名称')
project_id=serializer.PrimaryKeyRelatedField(queryset=Projects.objects.all(),label='项目id',help_text='项目id',)
class Meta:
model=Interfaces
fields=['update_time']
extra_kwargs={
"update_time":{
"format":"%Y年%m月%d日 %H:%M:%S"
},
"create_time":{
"read_only":True,
"format": "%Y年%m月%d日 %H:%M:%S"
}
}
def to_internal_value(self,data):
response=super().to_internal_value(data)
response['project_id']=response.get('project_id').id
return response
def to_internal_value(self,data):
response=super().to_internal_value(data)
response['project']=response.pop('project_id')
return response
需求2:获取接口下的所有用例数据
🔥🔥🔥特别注意4
接口表和用例表的映射关系为一对多 当主表获取从表的数据是,默认使用模型类名小写_set 当从表的外键字段中设置了related_name选项时,需要用related_name指定的名称
class TestcaseNameSerializer(ModelSerializer):
class Meta:
model=Testcases
fields=['id','name']
class InterfaceTestcaseSerializer(ModelSerializer):
testcases_set=TestcaseNameSerializer(label='接口下的用例',
help_text='接口下的用例',
read_only=True,
many=True)
class Meta:
model=Interfaces
fields=['testcases_set']
需求3:获取接口下的所有配置数据
class InterfaceConfigureSerializer(ModelSerializer):
class Meta:
model=Configures
fields=['id','name']
class InterfaceConfigureSerializer1(ModelSerializer):
configures=InterfaceConfigureSerializer(many=True,read_only=True)
class Meta:
model=Interfaces
fields=['configures']
视图设计
@action(methods=['GET'],detail=True)
def configs(self,request,*args,**kwargs):
response=super().retrieve(request,*args,**kwargs)
response.data=response.data.get('configures')
return response
@action(methods=['GET'],detail=True)
def testcases(self,request,*args,**kwargs):
response=super().retrieve(request,*args,**kwargs)
response.data=response.data.get('testcases_set')
return response
|