Django中的对象转化为Json对象返回---serializers.serialize()

# from django.shortcuts import render
#
# # Create your views here.
from django.http import HttpResponse,JsonResponse
from BombApp.models import *
from django.core import serializers
import json
def ver_data(request):

    resp = {}
    rev_data = VersionTable.objects.values()
    resp['ver'] = list(rev_data)
    simplelib_data = Simplelib.objects.all()
    # print(json.loads(serializers.serialize('json', simplelib_data)))
    bombinfo_data = Bombinfo.objects.all()
    versiontable_data = VersionTable.objects.all()
    categorytable_data = CategoryTable.objects.all()
    relationtable_data = RelationTable.objects.all()
    checktable_data = CheckTable.objects.all()

    # resp['data'] = json.loads(serializers.serialize('json', simplelib_data))
    resp['data'] = dict(
        simplelib=[d["fields"] for d in json.loads(serializers.serialize('json', simplelib_data))],
        bombinfo=[d["fields"] for d in json.loads(serializers.serialize('json', bombinfo_data))],
        versiontable=[d["fields"] for d in json.loads(serializers.serialize('json', versiontable_data))],
        categorytable=[d["fields"] for d in json.loads(serializers.serialize('json', categorytable_data))],
        relationtable=[d["fields"] for d in json.loads(serializers.serialize('json', relationtable_data))],
        checktable=[d["fields"] for d in json.loads(serializers.serialize('json', checktable_data))],
    )

    return JsonResponse(resp, safe=False)

Django中的对象转化为Json对象返回---serializers.serialize()_第1张图片

你可能感兴趣的:(Django)