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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

星河赵   2021-03-30 我要评论

平常的开发过程中不免遇到需要把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}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们