CSDN话题挑战赛第2期
参赛话题:学习笔记
案例:以项目表、接口表、用例表、配置表为例
模型类
项目模型类
classProjects(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='简要描述')classMeta:
db_table ='tb_projects'
verbose_name ='项目信息'
verbose_name_plural = verbose_name
ordering =('id',)def__str__(self):return self.name
接口模型类
classInterfaces(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='简要描述')classMeta:
db_table ='tb_interfaces'
verbose_name ='接口信息'
verbose_name_plural = verbose_name
ordering =('id',)def__str__(self):return self.name
用例模型类
classTestcases(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='请求信息')classMeta:
db_table ='tb_testcases'
verbose_name ='用例信息'
verbose_name_plural = verbose_name
ordering =('id',)def__str__(self):return self.name
配置模型类
classConfigures(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='请求信息')classMeta:
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)方法处理,在字段进行校验前最开始执行的方法
序列化器类设计
classInterfaceSerializer(serializers.ModelSerializer):
project=serializers.StringRelatedField(read_only=True,label='项目名称',help_text='项目名称')
project_id=serializer.PrimaryKeyRelatedField(queryset=Projects.objects.all(),label='项目id',help_text='项目id',)classMeta:
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"}}defto_internal_value(self,data):
response=super().to_internal_value(data)
response['project_id']=response.get('project_id').idreturn response
#或者defto_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指定的名称
classTestcaseNameSerializer(ModelSerializer):classMeta:
model=Testcases
fields=['id','name']classInterfaceTestcaseSerializer(ModelSerializer):
testcases_set=TestcaseNameSerializer(label='接口下的用例',
help_text='接口下的用例',
read_only=True,
many=True)classMeta:
model=Interfaces
fields=['testcases_set']
需求3:获取接口下的所有配置数据
classInterfaceConfigureSerializer(ModelSerializer):classMeta:
model=Configures
fields=['id','name']classInterfaceConfigureSerializer1(ModelSerializer):
configures=InterfaceConfigureSerializer(many=True,read_only=True)classMeta:
model=Interfaces
fields=['configures']
视图设计
@action(methods=['GET'],detail=True)defconfigs(self,request,*args,**kwargs):
response=super().retrieve(request,*args,**kwargs)
response.data=response.data.get('configures')return response
@action(methods=['GET'],detail=True)deftestcases(self,request,*args,**kwargs):
response=super().retrieve(request,*args,**kwargs)
response.data=response.data.get('testcases_set')return response
版权归原作者 YZL40514131 所有, 如有侵权,请联系我们删除。