django 将model转换为字典的方法示例
作者:星河赵 时间:2022-09-16 14:03:09
平常的开发过程中不免遇到需要把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
标签:Django,model,字典
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
怎么快速自学python
2023-06-25 19:19:32
DOM和XMLHttpRequest对象的属性和方法整理
2024-04-23 09:05:27
python机器学习Logistic回归原理推导
2022-12-17 00:45:15
![](https://img.aspxhome.com/file/2023/6/76696_0s.jpg)
Pycharm-community-2020.2.3 社区版安装教程图文详解
2022-07-02 06:30:00
![](https://img.aspxhome.com/file/2023/3/133503_0s.png)
Go语言中的数据竞争模式详解
2024-05-22 10:15:41
![](https://img.aspxhome.com/file/2023/9/123909_0s.png)
Python统计可散列的对象之容器Counter详解
2023-09-23 18:30:50
![](https://img.aspxhome.com/file/2023/3/70823_0s.png)
2个asp获取安全字符串的方法
2007-10-19 15:08:00
python 使用elasticsearch 实现翻页的三种方式
2021-03-09 17:39:57
![](https://img.aspxhome.com/file/2023/1/95271_0s.jpg)
解决python web项目意外关闭,但占用端口的问题
2023-02-11 19:18:53
kNN算法python实现和简单数字识别的方法
2023-09-05 21:44:36
![](https://img.aspxhome.com/file/2023/1/72751_0s.png)
Python中json.load()和json.loads()有哪些区别
2022-11-09 09:24:05
![](https://img.aspxhome.com/file/2023/7/78517_0s.png)
详解JS如何判断对象上是否存在某个属性
2024-04-16 09:49:28
Pandas如何对Categorical类型字段数据统计实战案例
2023-03-01 20:02:26
如何去除点击链接时出现的虚线框
2007-12-02 17:38:00
python list 查询是否存在并且并返回下标的操作
2023-06-20 12:05:43
![](https://img.aspxhome.com/file/2023/5/84105_0s.jpg)
Python pandas RFM模型应用实例详解
2023-10-15 23:27:34
![](https://img.aspxhome.com/file/2023/7/76977_0s.png)
python清除字符串中间空格的实例讲解
2023-12-29 22:40:24
Python基于HOG+SVM/RF/DT等模型实现目标人行检测功能
2021-07-12 01:54:50
![](https://img.aspxhome.com/file/2023/2/105862_0s.jpg)
python密码学对称和非对称密码教程
2023-06-19 15:56:59
![](https://img.aspxhome.com/file/2023/5/67785_0s.jpg)
利用types增强vscode中js代码提示功能详解
2023-07-15 05:57:08
![](https://img.aspxhome.com/file/2023/1/55981_0s.png)