通过代码简单了解django model序列化作用

一直对使用DRF的了解停留在一知半解的状态,今天在实际操作中,感受到了DRF带来的方便

Django工程,其中两个model定义如下:

AutomationHeadRaw:

class AutomationHeadRaw(models.Model):

"""

测试用例的请求的json形式参数

"""

id = models.AutoField(primary_key=True)

automationCaseApi = models.OneToOneField(AutomationCaseApi, related_name='headRaw',

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格式参数管理'

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='接口名称')

httpType = models.CharField(max_length=50, default='HTTP', verbose_name='HTTP/HTTPS', choices=HTTP_CHOICE)

requestType = models.CharField(max_length=50, verbose_name='请求方式', choices=REQUEST_TYPE_CHOICE)

apiAddress = models.CharField(max_length=1024, verbose_name='接口地址')

requestParameterType = models.CharField(max_length=50, verbose_name='参数请求格式', choices=REQUEST_PARAMETER_TYPE_CHOICE)

headType = models.CharField(max_length=50, verbose_name='请求头部格式', choices=REQUEST_PARAMETER_TYPE_CHOICE)

formatRaw = models.BooleanField(default=False, verbose_name="是否转换成源数据")

examineType = models.CharField(default='no_check', max_length=50, verbose_name='校验方式', choices=EXAMINE_TYPE_CHOICE)

httpCode = models.CharField(max_length=50, blank=True, null=True, verbose_name='HTTP状态', choices=HTTP_CODE_CHOICE)

responseData = models.TextField(blank=True, null=True, verbose_name='返回内容')

# 新增用例相关参数

preFun = models.CharField(max_length=1024, blank=True, null=True, verbose_name='前置函数')

afterFun = models.CharField(max_length=1024, blank=True, null=True, verbose_name='后置函数')

skipFlag = models.CharField(max_length=50, blank=True, null=True, verbose_name='跳过标识', choices=Y_N_CHOICE)

stopFlag = models.CharField(max_length=50, blank=True, null=True, verbose_name='中断标识', choices=Y_N_CHOICE)

retryNum = models.IntegerField(verbose_name='重试次数', default=1)

def __unicode__(self):

return self.name

def __str__(self):

return self.name

class Meta:

verbose_name = '用例接口'

verbose_name_plural = '用例接口管理'

1、手工转换获取到了AutomationHeadRaw模型中的data数据(json格式)

需求为取AutomationHeadRaw模型中的data数据(json格式),我在使用的时候,没有给AutomationHeadRaw建立对应的序列化类,取数时使用一下数据获取data数据:

head_test = AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id)

self.header =json.loads(json.loads(serializers.serialize('json', head))[0]["fields"]["data"])

手工转换获取到了AutomationHeadRaw模型中的data数据(json格式)

2、自动转换获取到了AutomationCaseApi模型中的data数据

另一个模型AutomationCaseApi ,定义了对应的model序列化类AutomationCaseApiSerializer如下:

class AutomationCaseApiSerializer(serializers.ModelSerializer):

"""

自动化用例接口详细信息序列化

"""

headers = AutomationHeadSerializer(many=True, read_only=True)

headRaw = AutomationHeadRawSerializer(many=False, read_only=True) # 一对一表格,变量名一定要和model定义relate-name一直

parameterList = AutomationParameterSerializer(many=True, read_only=True)

parameterRaw = AutomationParameterRawSerializer(many=False, read_only=True)

class Meta:

model = AutomationCaseApi

fields = ('id', 'name', 'httpType', 'requestType', 'apiAddress', 'headers', 'headType', 'requestParameterType',

'headRaw', 'formatRaw', 'parameterList', 'parameterRaw', 'examineType', 'httpCode', 'responseData', 'preFun',

'afterFun', 'skipFlag', 'stopFlag', 'retryNum')

则获取模型AutomationCaseApi可以自动转换:

self.case_data = AutomationCaseApiSerializer(

AutomationCaseApi.objects.get(id=self.case_api_id, automationTestCase=self.case_id)).data

3、因此上面的AutomationHeadRaw 可以通过添加model序列化类AutomationHeadRawSerializer自动转换数据格式:

class AutomationHeadRawSerializer(serializers.ModelSerializer):

自动化用例接口json类型请求头信息序列化

class Meta:

model = AutomationHeadRaw

fields = ('id', 'automationCaseApi', 'data')

获取数据语句可以改成,不需要手工转换:

head = AutomationHeadRawSerializer(

AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id),

many=True).data

注意:

上面获取数据的AutomationHeadRawSerializer()方法,入参1 :AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id)

可以为两种对象:

AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id) (filter方法对象为queryset类型)

AutomationCaseApi.objects.get(id=self.case_api_id, automationTestCase=self.case_id)(get方法对象为model类 类型)

以上是 通过代码简单了解django model序列化作用 的全部内容, 来源链接: utcz.com/z/347713.html

回到顶部