Python 在Django中使用dict追加列表
我有一个疑问:Python 在Django中使用dict追加列表,python,django,Python,Django,我有一个疑问: for myid in ids_high: getgeom = FloodHazard.objects.get(id=myid).geom response_high = list(BuildingStructure.objects.filter(geom__intersects=getgeom).values( 'brgy_locat','bldg_type','bldg_name').annotate(counthigh=Count('brgy
for myid in ids_high:
getgeom = FloodHazard.objects.get(id=myid).geom
response_high = list(BuildingStructure.objects.filter(geom__intersects=getgeom).values(
'brgy_locat','bldg_type','bldg_name').annotate(counthigh=Count('brgy_locat')))
to_json.append(response_high)
其结果是:
[
{
"counthigh": 1,
"brgy_locat": "Tolosa",
"bldg_name": "",
"bldg_type": ""
},
{
"counthigh": 7,
"brgy_locat": "Barangay 9",
"bldg_name": "",
"bldg_type": ""
},
{
"counthigh": 3,
"brgy_locat": "Mabini",
"bldg_name": "",
"bldg_type": ""
}
]
to_json
是一个列表,我想要一个dict
例如[“type”:“High”]。怎么用?到目前为止,我尝试了附加和扩展。如何附加两次?可能吗
预期输出如下所示:
{
"counthigh": 3,
"brgy_locat": "Mabini",
"bldg_name": "",
"bldg_type": "",
"type":"high"
}
依此类推……您应该能够在response\u high上迭代,并将键
type
添加到每个条目中
for myid in ids_high:
getgeom = FloodHazard.objects.get(id=myid).geom
response_high = list(BuildingStructure.objects.filter(geom__intersects=getgeom).values(
'brgy_locat','bldg_type','bldg_name').annotate(counthigh=Count('brgy_locat')))
for entry in response_high:
entry['type'] = 'high'
to_json.append(response_high)
这是一份口述清单。“类型”与任何事情都有什么关系?它从哪里来?一个固定值..这样它将输出:
“counthigh”:3,“brgy\u locat”:“Mabini”,“bldg\u name”:“bldg\u type”:“type”:“High”
“type”是一个模型字段还是取决于整数counthigh?您想附加两次什么?您可能应该添加所需的示例输出。否..类型不是模型字段,我只想附加列表。