Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django中使用dict追加列表_Python_Django - Fatal编程技术网

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?您想附加两次什么?您可能应该添加所需的示例输出。否..类型不是模型字段,我只想附加列表。