Django 组织json格式

@api_view([GET, POST])
def api_test(request):
    classes = Classes.objects.all()
    # classes_data = Classes_data(classes, many=True)
    # userlist = Userinfo.objects.all()
    # userlist_data = Userinfo_data(userlist, many=True)
    #
    # data = {
    #     ‘classes‘: classes_data.data,
    #     ‘userlist‘: userlist_data.data,
    # }
    data = {
        classes: [],
    }

    for c in classes:
        data_item = {
            id: c.id,
            text: c.text,
            userlist: [],
        }
        userlist = c.userinfo_classes.all()
        for user in userlist:
            user_data = {
                id: user.id,
                nickName: user.nickName,
                headImg: str(user.headImg)
            }
            data_item[userlist].append(user_data)
        data[classes].append(data_item)
    # data = json.dumps(data)
    return Response(data)

 

Django 组织json格式

上一篇:《深入Java虚拟机学习笔记》- 第16章 控制流


下一篇:PHP设计模式之路-单件模式(Singleton Pattern)