久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

通過代碼簡單了解django model序列化作用

瀏覽:132日期:2024-09-18 17:44:34

一直對使用DRF的了解停留在一知半解的狀態(tài),今天在實(shí)際操作中,感受到了DRF帶來的方便

Django工程,其中兩個model定義如下:

AutomationHeadRaw:class AutomationHeadRaw(models.Model):'''測試用例的請求的json形式參數(shù)'''id = models.AutoField(primary_key=True)automationCaseApi = models.OneToOneField(AutomationCaseApi, related_name=’headRaw’,on_delete=models.CASCADE, verbose_name=’接口’)data = models.TextField(verbose_name=’源數(shù)據(jù)請求頭json數(shù)據(jù)’, blank=True, null=True)class Meta:verbose_name = ’請求頭json格式參數(shù)’verbose_name_plural = ’請求頭json格式參數(shù)管理’

AutomationCaseApi:

class AutomationCaseApi(models.Model): ''' 用例執(zhí)行接口 ''' 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=’參數(shù)請求格式’, 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='是否轉(zhuǎn)換成源數(shù)據(jù)') examineType = models.CharField(default=’no_check’, max_length=50, verbose_name=’校驗(yàn)方式’, choices=EXAMINE_TYPE_CHOICE) httpCode = models.CharField(max_length=50, blank=True, null=True, verbose_name=’HTTP狀態(tài)’, choices=HTTP_CODE_CHOICE) responseData = models.TextField(blank=True, null=True, verbose_name=’返回內(nèi)容’) # 新增用例相關(guān)參數(shù) preFun = models.CharField(max_length=1024, blank=True, null=True, verbose_name=’前置函數(shù)’) afterFun = models.CharField(max_length=1024, blank=True, null=True, verbose_name=’后置函數(shù)’) skipFlag = models.CharField(max_length=50, blank=True, null=True, verbose_name=’跳過標(biāo)識’, choices=Y_N_CHOICE) stopFlag = models.CharField(max_length=50, blank=True, null=True, verbose_name=’中斷標(biāo)識’, choices=Y_N_CHOICE) retryNum = models.IntegerField(verbose_name=’重試次數(shù)’, default=1) def __unicode__(self): return self.name def __str__(self): return self.name class Meta: verbose_name = ’用例接口’ verbose_name_plural = ’用例接口管理’

1、手工轉(zhuǎn)換獲取到了AutomationHeadRaw模型中的data數(shù)據(jù)(json格式)

需求為取AutomationHeadRaw模型中的data數(shù)據(jù)(json格式),我在使用的時候,沒有給AutomationHeadRaw建立對應(yīng)的序列化類,取數(shù)時使用一下數(shù)據(jù)獲取data數(shù)據(jù):head_test = AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id)self.header =json.loads(json.loads(serializers.serialize(’json’, head))[0]['fields']['data'])

手工轉(zhuǎn)換獲取到了AutomationHeadRaw模型中的data數(shù)據(jù)(json格式)

2、自動轉(zhuǎn)換獲取到了AutomationCaseApi模型中的data數(shù)據(jù)

另一個模型AutomationCaseApi ,定義了對應(yīng)的model序列化類AutomationCaseApiSerializer如下:

class AutomationCaseApiSerializer(serializers.ModelSerializer): ''' 自動化用例接口詳細(xì)信息序列化 ''' 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可以自動轉(zhuǎn)換:

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

3、因此上面的AutomationHeadRaw 可以通過添加model序列化類AutomationHeadRawSerializer自動轉(zhuǎn)換數(shù)據(jù)格式:class AutomationHeadRawSerializer(serializers.ModelSerializer):

自動化用例接口json類型請求頭信息序列化

class Meta:model = AutomationHeadRawfields = (’id’, ’automationCaseApi’, ’data’)

獲取數(shù)據(jù)語句可以改成,不需要手工轉(zhuǎn)換:head = AutomationHeadRawSerializer(AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id),many=True).data

注意:

上面獲取數(shù)據(jù)的AutomationHeadRawSerializer()方法,入?yún)? :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類 類型)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 定安县| 福建省| 甘肃省| 湾仔区| 许昌县| 鲜城| 崇礼县| 广丰县| 郴州市| 德江县| 中西区| 修文县| 昆明市| 仪陇县| 浠水县| 安庆市| 旅游| 梁山县| 塘沽区| 金塔县| 兰考县| 渭源县| 齐齐哈尔市| 勃利县| 太原市| 会昌县| 镇坪县| 大宁县| 云安县| 广宁县| 晴隆县| 嘉黎县| 连山| 西藏| 榆林市| 宁明县| 汝南县| 都昌县| 苍山县| 海兴县| 稻城县|