通過代碼簡單了解django model序列化作用
一直對使用DRF的了解停留在一知半解的狀態(tài),今天在實(shí)際操作中,感受到了DRF帶來的方便
Django工程,其中兩個(gè)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格式),我在使用的時(shí)候,沒有給AutomationHeadRaw建立對應(yīng)的序列化類,取數(shù)時(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、自動(dòng)轉(zhuǎn)換獲取到了AutomationCaseApi模型中的data數(shù)據(jù)
另一個(gè)模型AutomationCaseApi ,定義了對應(yīng)的model序列化類AutomationCaseApiSerializer如下:
class AutomationCaseApiSerializer(serializers.ModelSerializer): ''' 自動(dòng)化用例接口詳細(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可以自動(dòng)轉(zhuǎn)換:
self.case_data = AutomationCaseApiSerializer(AutomationCaseApi.objects.get(id=self.case_api_id, automationTestCase=self.case_id)).data
3、因此上面的AutomationHeadRaw 可以通過添加model序列化類AutomationHeadRawSerializer自動(dòng)轉(zhuǎn)換數(shù)據(jù)格式:class AutomationHeadRawSerializer(serializers.ModelSerializer):
自動(dòng)化用例接口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)。
相關(guān)文章:
1. asp批量添加修改刪除操作示例代碼2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. PHP循環(huán)與分支知識點(diǎn)梳理4. 讀大數(shù)據(jù)量的XML文件的讀取問題5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)7. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能8. 解析原生JS getComputedStyle9. jsp+servlet實(shí)現(xiàn)猜數(shù)字游戲10. css代碼優(yōu)化的12個(gè)技巧
