1

问题:我有一个带有 3 个外键的数据库,每天我必须按照以下规则收集用户输入;

  1. 如果数据库中已经存在今天日期的数据,则检索并显示数据。数据将产生多行,但每一行都是基于 3 个外键(product、branch、merge_version)和 date_created(auto_add=True)唯一查询的

  2. 如果查询没有返回任何内容,则使用类实例创建一个对象并显示空行

  3. 如果用户单击一个提交按钮,则数据库中的数据应按照以下规则更新;

    • 对于提交的每一行,验证数据库中是否已经存在现有行,如果存在则更新该行,否则创建一个新行

我尝试在 for 循环中为每个产品创建多个表单实例,并将字典发送到模板页面,它显示行没有任何问题。

当我单击提交时,仅保存最后一行数据,因为 request.POST 返回以下输出;

    QueryDict: {u'build_date': [u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'2013-10-11', u'
    2013-10-11'], u'cln': [u'5555', u'2222', u'2222', u'2222', u'2222', u'2222', u'2222', u'090909'], u'logs': [u'no logs', u'no logs', u'no logs', u'no logs', u'no logs', u'n
    o logs', u'no logs', u'no logs'], u'rpmt_status': [u'R', u'R', u'R', u'R', u'R', u'R', u'R', u'R'], u'rpmt_result': [u'F', u'F', u'F', u'F', u'F', u'F', u'F', u'F'], u'com
    ments': [u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg', u'hghjg'], u'cbs_other_bugs': [u'666', u'666', u'666', u'666', u'666', u'666', u'666', u'666
    '], u'build_num': [u'12345', u'4444', u'8888', u'8888', u'8888', u'8888', u'8888', u'080808'], u'cbs_merge_blocker': [u'76767', u'76767', u'76767', u'76767', u'76767', u'7
    6767', u'76767', u'76767'], u'csrfmiddlewaretoken': [u'8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw'], u'cbs_waived': [u'89898', u'89898', u'89898', u'89898', u'89898', u'89898', u'89
    898', u'89898']}

如果我每行都有单独的提交按钮,我会得到以下 request.POST

        QueryDict: {u'build_date': [u'2013-10-11'], u'cln': [u'23423'], u'logs': [u'no logs'], u'rpmt_status': [u'R'], u'rpmt_result': [u'F'], u'comments': [u'hghjg'], u'cbs_other_bugs': [u'666'], u'build_num': [u'12345'], u'cbs_merge_blocker': [u'76767'], u'csrfmiddlewaretoken': [u'8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw'], u'cbs_waived': [u'89898']}

但我想用一个提交按钮保存整个数据库。请告诉我我在哪里犯错?或者除了使用 ModelForm 之外,有没有最好的方法来处理这种情况?

4

1 回答 1

1

基本上问题出在客户端,它发送发布请求。数据在客户端没有正确打包!

创建此 json 数据的 javascript 不遵守服务器端预期的数据交换格式。

解决方案是以正确的字典格式打包数据。这就是旧数据格式的样子;

{
    "build_date": [
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11",
        "2013-10-11"
    ],
    "build_num": [
        "12345",
        "4444",
        "8888",
        "8888",
        "8888",
        "8888",
        "8888",
        "080808"
    ],
    "cbs_merge_blocker": [
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767",
        "76767"
    ],
    "cbs_other_bugs": [
        "666",
        "666",
        "666",
        "666",
        "666",
        "666",
        "666",
        "666"
    ],
    "cbs_waived": [
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898",
        "89898"
    ],
    "cln": [
        "5555",
        "2222",
        "2222",
        "2222",
        "2222",
        "2222",
        "2222",
        "090909"
    ],
    "comments": [
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg",
        "hghjg"
    ],
    "csrfmiddlewaretoken": [
        "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
    ],
    "logs": [
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs",
        "no logs"
    ],
    "rpmt_result": [
        "F",
        "F",
        "F",
        "F",
        "F",
        "F",
        "F",
        "F"
    ],
    "rpmt_status": [
        "R",
        "R",
        "R",
        "R",
        "R",
        "R",
        "R",
        "R"
    ]
}

我建议将其更改为添加一个称为产品的更多级别的键(主键),并且所有数组值都与一个特定产品相关。所以它看起来像这样!

{
   "product1":{
      "build_date":[
         "2013-10-11"
      ],
      "cln":[
         "23423"
      ],
      "comments":[
         "hghjg"
      ],
      "cbs_merge_blocker":[
         "76767"
      ],
      "rpmt_status":[
         "R"
      ],
      "logs":[
         "no logs"
      ],
      "cbs_other_bugs":[
         "666"
      ],
      "csrfmiddlewaretoken":[
         "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
      ],
      "build_num":[
         "12345"
      ],
      "cbs_waived":[
         "89898"
      ],
      "rpmt_result":[
         "F"
      ]
   },
   "product2":{
      "build_date":[
         "2013-10-11"
      ],
      "cln":[
         "23423"
      ],
      "comments":[
         "hghjg"
      ],
      "cbs_merge_blocker":[
         "76767"
      ],
      "rpmt_status":[
         "R"
      ],
      "logs":[
         "no logs"
      ],
      "cbs_other_bugs":[
         "666"
      ],
      "csrfmiddlewaretoken":[
         "8sNCLiUu23tvqLe1QHdJp1c5sQJmhevw"
      ],
      "build_num":[
         "12345"
      ],
      "cbs_waived":[
         "89898"
      ],
      "rpmt_result":[
         "F"
      ]
   }
}
于 2016-07-16T20:38:25.420 回答