django 將model轉(zhuǎn)換為字典的方法示例
更新時間:2018年10月16日 08:37:35 作者:星河趙
平常的開發(fā)過程中不免遇到需要把model轉(zhuǎn)成字典的需求,這篇文章主要介紹了Django model轉(zhuǎn)字典的方法,非常具有實用價值,需要的朋友可以參考下
平常的開發(fā)過程中不免遇到需要把model轉(zhuǎn)成字典的需求,尤其是現(xiàn)在流行前后端分離架構(gòu),Json格式幾乎成了前后端之間數(shù)據(jù)交換的標準,這種model轉(zhuǎn)dict的需求就更多了,本文介紹日常使用的方法以供參考
from django.forms.models import model_to_dict
from projects.models import ProjectInformation
site = ProjectInformation.objects.get(id=6)
dict = model_to_dict(site)
dict
{'CRFmethod': '',
'EDCprovider': '',
'acceptancenum': '',
'add_time': datetime.datetime(2017, 4, 20, 8, 4, 42, 751202, tzinfo=<UTC>),
'begindate': None,
'clinicalassis': '',
'clinicalnum': '',
'created_by': '',
'created_date': None,
'enddate': None,
'ethicsreviewdate': None,
'ethicsreviewpers': '',
'ethicsreviewres': '',
'ethicsreviewunit': '',
'id': 6,
'isimport': None,
'leaderunit': None,
'localcases': None,
'medicalequipment': '',
'mequipmenttype': '',
'multicenter': '',
'plannum': '',
'proenname': '愛上地方',
'proname': '打士大夫',
'prostatus': '',
'prosummary': '',
'protype': '打是否',
'regstudy': '是',
'reportdate': None,
'reportnum': '',
'reportversion': '',
'researchdesign': '',
'researchtype': '',
'responsible': '',
'studytype': '器械類',
'telephonenum': None,
'totalcases': None,
'treatmenttype': None,
'unitnum': None}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用Pandas實現(xiàn)Excel讀取
這篇文章主要為大家介紹了在Python中如何調(diào)用Pandas實現(xiàn)Excel文件的讀取,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-04-04
python中for循環(huán)輸出列表索引與對應(yīng)的值方法
今天小編就為大家分享一篇python中for循環(huán)輸出列表索引與對應(yīng)的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11

