django serializers to json - custom json output format

前端 未结 4 732
野的像风
野的像风 2021-02-04 19:23

I am quite new to django and recently I have a requirement of a JSON output, for which I use the following django code:

data = serializers.serialize(\"json\", My         


        
4条回答
  •  我寻月下人不归
    2021-02-04 19:52

    import json
    
    _all_data = Reporter.objects. all()
    
    json_data = json.dumps([{'name': reporter.full_name} for reporter in _all_data])
    
    return HttpResponse(json_data, content_type='application/json')
    

    Here Reporter is your Model

提交回复
热议问题