代码之家  ›  专栏  ›  技术社区  ›  Nilay Singh

Django用json显示列表数据

  •  0
  • Nilay Singh  · 技术社区  · 6 年前

    我用django将pandas数据帧显示为json格式我起诉django panda我可以将我的模型转换为数据帧,但我不知道如何用json格式显示整个帧。我正在使用此代码:

    def index(request):
    qs = DebtManagement1S.objects.all()
    df = qs.to_dataframe()
    resp_data = {
        'x': list(df['sector']),
        'z': df.to_json()
        }
    return JsonResponse(resp_data)
    

    它又回来了 Json response

     {
       sector: {
                "Internal Debt",
                 "Central Loans"
             ...
               }
      }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Daniel Roseman    6 年前

    你的 resp_data dict需要由Python数据结构组成。所以你应该打电话 df.to_dict() .