通過(guò)代碼簡(jiǎn)單了解django model序列化作用
一直對(duì)使用DRF的了解停留在一知半解的狀態(tài),今天在實(shí)際操作中,感受到了DRF帶來(lái)的方便
Django工程,其中兩個(gè)model定義如下:
AutomationHeadRaw: class AutomationHeadRaw(models.Model): """ 測(cè)試用例的請(qǐng)求的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ù)請(qǐng)求頭json數(shù)據(jù)', blank=True, null=True) class Meta: verbose_name = '請(qǐng)求頭json格式參數(shù)' verbose_name_plural = '請(qǐng)求頭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='請(qǐng)求方式', choices=REQUEST_TYPE_CHOICE) apiAddress = models.CharField(max_length=1024, verbose_name='接口地址') requestParameterType = models.CharField(max_length=50, verbose_name='參數(shù)請(qǐng)求格式', choices=REQUEST_PARAMETER_TYPE_CHOICE) headType = models.CharField(max_length=50, verbose_name='請(qǐng)求頭部格式', 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='跳過(guò)標(biāo)識(shí)', choices=Y_N_CHOICE) stopFlag = models.CharField(max_length=50, blank=True, null=True, verbose_name='中斷標(biāo)識(shí)', 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í)候,沒(méi)有給AutomationHeadRaw建立對(duì)應(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 ,定義了對(duì)應(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) # 一對(duì)一表格,變量名一定要和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 可以通過(guò)添加model序列化類AutomationHeadRawSerializer自動(dòng)轉(zhuǎn)換數(shù)據(jù)格式:
class AutomationHeadRawSerializer(serializers.ModelSerializer):
自動(dòng)化用例接口json類型請(qǐng)求頭信息序列化
class Meta:
model = AutomationHeadRaw
fields = ('id', 'automationCaseApi', 'data')獲取數(shù)據(jù)語(yǔ)句可以改成,不需要手工轉(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)
可以為兩種對(duì)象:
AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id) (filter方法對(duì)象為queryset類型)
AutomationCaseApi.objects.get(id=self.case_api_id, automationTestCase=self.case_id)(get方法對(duì)象為model類 類型)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3中使用__slots__限定實(shí)例屬性操作分析
這篇文章主要介紹了python3中使用__slots__限定實(shí)例屬性操作,結(jié)合實(shí)例形式分析了Python3定義類實(shí)例綁定屬性,以及使用__slots__限定實(shí)例屬性的相關(guān)操作技巧,需要的朋友可以參考下2020-02-02python3調(diào)用ansible?api使用實(shí)例例說(shuō)明
這篇文章主要為大家介紹了python3?調(diào)用ansible?api使用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07python 安裝virtualenv和virtualenvwrapper的方法
下面小編就為大家?guī)?lái)一篇python 安裝virtualenv和virtualenvwrapper的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01使用python動(dòng)態(tài)生成波形曲線的實(shí)現(xiàn)
今天小編就為大家分享一篇使用python動(dòng)態(tài)生成波形曲線的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python使用matplotlib簡(jiǎn)單繪圖示例
這篇文章主要介紹了Python使用matplotlib簡(jiǎn)單繪圖,結(jié)合實(shí)例形式分析了Python基于matplotlib繪制正弦與余弦曲線相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python入門(mén)while循環(huán)語(yǔ)句理解學(xué)習(xí)
這篇文章主要介紹了python入門(mén)while循環(huán)語(yǔ)句理解學(xué)習(xí),文中附含詳細(xì)圖文示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09Python 多線程共享變量的實(shí)現(xiàn)示例
這篇文章主要介紹了Python 多線程共享變量的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Python解析、提取url關(guān)鍵字的實(shí)例詳解
今天小編就為大家分享一篇Python解析、提取url關(guān)鍵字的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12你們要的Python繪畫(huà)3D太陽(yáng)系詳細(xì)代碼
這篇文章主要給大家介紹了關(guān)于如何利用Python 繪畫(huà)3D太陽(yáng)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-10-10解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03