通過代碼簡單了解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ù)據請求頭json數(shù)據', 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="是否轉換成源數(shù)據")
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='返回內容')
# 新增用例相關參數(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、手工轉換獲取到了AutomationHeadRaw模型中的data數(shù)據(json格式)
需求為取AutomationHeadRaw模型中的data數(shù)據(json格式),我在使用的時候,沒有給AutomationHeadRaw建立對應的序列化類,取數(shù)時使用一下數(shù)據獲取data數(shù)據:
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數(shù)據(json格式)
2、自動轉換獲取到了AutomationCaseApi模型中的data數(shù)據
另一個模型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自動轉換數(shù)據格式:
class AutomationHeadRawSerializer(serializers.ModelSerializer):
自動化用例接口json類型請求頭信息序列化
class Meta:
model = AutomationHeadRaw
fields = ('id', 'automationCaseApi', 'data')獲取數(shù)據語句可以改成,不需要手工轉換:
head = AutomationHeadRawSerializer(
AutomationHeadRaw.objects.filter(automationCaseApi=self.case_api_id),
many=True).data
注意:
上面獲取數(shù)據的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數(shù)據庫makemigrations有變化但是migrate時未變動問題
今天小編就為大家分享一篇解決Django數(shù)據庫makemigrations有變化但是migrate時未變動的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python打包文件夾的方法小結(zip,tar,tar.gz等)
這篇文章主要介紹了Python打包文件夾的方法,結合實例形式總結分析了Python打包成zip,tar,tar.gz等格式文件的操作技巧,需要的朋友可以參考下2016-09-09
Python實現(xiàn)Excel文件的合并(以新冠疫情數(shù)據為例)
這篇將以新冠疫情數(shù)據為例,詳細介紹了如何利用Python實現(xiàn)合并Excel文件,文中的示例代碼講解詳細,感興趣的可以了解一下2022-03-03

