[Answer]-Use HttpResponse with JSON data in this code

1👍

There is no much sense in your code. You assign all querysets to the single key in the response dict. You should use a list for this purpose:

As far as I understand the code should be something like this:

response = []
for myid in ids:
    getgeom = FloodHazard.objects.get(id=myid).geom
    response.extend(BuildingStructure.objects.filter(geom__intersects=getgeom)
                                     .values('brgy_locat')
                                     .annotate(counthigh=Count('brgy_locat')))

json_post = ujson.dumps(response)

If you want to return a hazard level as well as the list of buildings then you can return a dict:

json_post = ujson.dumps({'hazard': 'high', 'buildings': response})

Leave a comment