【发布时间】:2021-12-31 03:31:42
【问题描述】:
我定义了下面的类来为投票系统做统计。
class FormPage(AbstractForm):
submission_stats = models.JSONField(null=True, editable=False)
现在,我有以下格式的submission_stats:
[
{
"id":4,
"label":"checkboxes",
"choices":[
{
"content":"option-A",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-B",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-C",
"num_vote":0,
"user_list":[
]
}
]
},
{
"id":7,
"label":"Radio Button",
"choices":[
{
"content":"option-1",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-2",
"num_vote":0,
"user_list":[
]
},
{
"content":"option-3",
"num_vote":0,
"user_list":[
]
}
]
}
]
例如,当我收到 2 个投票提交时,我想相应地更新此 JSONField 中的 num_vote(从 0 到 2)和 user_list(从 [] 到 [user_a, user_b])字段。
请问如何查询和更新嵌套 JSONField 数据中的元素?
【问题讨论】:
-
为什么你的表格中有
mdels.JsonField?您想在哪里更新值? -
更新了帖子以反映我想要更新值的位置。
form中的这个models.JsonField属性用于统计表单提交结果。例如,有多少用户为选项 A 投票,这些用户是谁。
标签: python django django-jsonfield jsonfield