你说什么都对

  • 首页
  • 技术
  • 工具分享
  1. 首页
  2. 技术
  3. Python
  4. Django
  5. 正文

Django根据字段自动从dict中获取值

2022年11月3日 224点热度 0人点赞 0条评论
def getTableDict(tableName, totalDict):
    col = eval(tableName)._meta.fields
    col_name_list = [col[i].name for i in range(len(col))]
    tb_dict = {}
    for cnl in col_name_list:
        if cnl.upper() == 'ID' or cnl.upper() == 'PK':
            continue
        tb_dict[cnl] = totalDict[cnl]
    return tb_dict

tableName跟Model定义的名称一致即可
totalDict的字典keys要大于等于table定义的fields

标签: Python
最后更新:2022年11月3日

老虎猪

健康、快乐、品质生活

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

最近评论
最新 热点 随机
最新 热点 随机
Jenkins部署 在OneNote里使用markdown语法-OneMark 群晖使用花生壳搭建WordPress bootstrap table 图标显示解决 bootstrap div 居中显示 Django根据字段自动从dict中获取值
PHP将获取的时间显示为你想要的方式 bootstrap div 居中显示 Eclipse配置PHP开发环境+XDebug调试环境 Win10配置PHP开发环境 使用python-opencv在图像中查找最亮点 在OneNote里使用markdown语法-OneMark

COPYRIGHT © 2022 你说什么都对. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙公网安备 33022602000616号