gpt4 book ai didi

python - Django1.5 - python2.7 -如何显示和更新具有3个外键的数据库

转载 作者:太空宇宙 更新时间:2023-11-03 18:46:16 24 4
gpt4 key购买 nike

问题:我有一个有 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']}

如果我每行都有单独的提交按钮,我会收到以下请求。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 之外还有处理这种情况的最佳方法吗?

最佳答案

基本上问题出在发送 post 请求的客户端。数据在客户端未正确打包!

创建此 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"
]
}
}

关于python - Django1.5 - python2.7 -如何显示和更新具有3个外键的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19448199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com