通過代碼簡單了解django model序列化作用
一直對使用DRF的了解停留在一知半解的狀態(tài),今天在實際操作中,感受到了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='校驗方式', 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='跳過標識', 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='重試次數(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): """ 自動化用例接口詳細信息序列化 """ 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 = AutomationHeadRaw
fields = ('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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3調(diào)用ansible?api使用實例例說明
這篇文章主要為大家介紹了python3?調(diào)用ansible?api使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07python 安裝virtualenv和virtualenvwrapper的方法
下面小編就為大家?guī)硪黄猵ython 安裝virtualenv和virtualenvwrapper的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01使用python動態(tài)生成波形曲線的實現(xiàn)
今天小編就為大家分享一篇使用python動態(tài)生成波形曲線的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12解決python 輸出到csv 出現(xiàn)多空行的情況
這篇文章主要介紹了解決python 輸出到csv 出現(xiàn)多空行的情況,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03