网络编程
位置:首页>> 网络编程>> Python编程>> django 将model转换为字典的方法示例

django 将model转换为字典的方法示例

作者:星河赵  发布时间:2022-09-16 14:03:09 

标签:Django,model,字典

平常的开发过程中不免遇到需要把model转成字典的需求,尤其是现在流行前后端分离架构,Json格式几乎成了前后端之间数据交换的标准,这种model转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}

来源:https://www.cnblogs.com/zhaoyingjie/p/6742879.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com