Django rest API格式化数据

时间:2019-05-23 10:16:37

标签: json django api django-rest-framework

我是Django rest API的新手。我为我的项目创建了一个API,用户可以在其中通过API从系统获取数据。我对数据进行序列化并传递,但是我也想对结果进行计数并传递该值。

我的 view.py 在这里

    def categorySearch(request, slug):
        itemViewCategory = Item.objects.raw('''select *, company.slug as companySlug, company.name as companyName, 
        field.id as fieldId, field.name as fieldName, category.name as categoryName from company 
        inner join category on company.business_type = category.id inner join category_field on 
        category_field.category_id = category.id inner join custom_field as field on category_field.field_id = field.id 
        where category.id = (select id from category where slug= %s) and field.name LIKE %s order by company.name ''', [slug, product])

        itemViewCategoryCount = Item.objects.raw(
            '''select *, COUNT(company.name) as companyCount from company inner join category on 
             company.business_type = category.id where category.id = (select id from category where slug= %s) 
             order by company.name ''', [slug])

        results = []
        for val in itemViewCategory:
            place_json = {}
            place_json['name'] = val.name
            place_json['email'] = val.email
            place_json['address'] = val.address
            results.append(place_json)
        return JsonResponse(results, safe=False)

它的返回数据是这样的

[
    {
        "name": "Washing Plants",
        "email": "ababil.washing@yahoo.com",
        "address": "ababil.washing@yahoo.com"
    },
    {
        "name": "Washing Plants",
        "email": "info@dekkoisho.com",
        "address": "Holding # 79/1, Chandra, Kaliakoir"
    },
    {
        "name": "Washing Plants",
        "email": "aznmery90@yahoo.com",
        "address": "79/8/2 Bibir Bagicha ( 4 No. Gate )\r\nNorth Jatrabari, Dhaka-1205."
    },

]

但是我想要这种类型的数据

    {

        "statuscode": "0",

        "message": "Success",

        "data": [

            {

            "companyName": "Adorn Knitwear Ltd.",

            "phone": "01713047421",

            "city": "Dhaka"

        },

        {

            "companyName": "Adury Apparels Ltd.",

            "phone": "029333274",

            "city": "Dhaka"

        },

        {

            "companyName": "Advance World Ltd.",

            "phone": "01711537851",

            "city": "Dhaka"

        },

        {

            "companyName": "Afaku Apparels Ltd.",

            "phone": "01711869977",

            "city": "Dhaka"

           }

    ],

    "common": {

        "totalCompany": "104",

    }

}

现在我该如何格式化我的数据?

1 个答案:

答案 0 :(得分:0)

    for val in itemViewCategory:
            place_json = {}
            place_json['name'] = val.name
            place_json['email'] = val.email
            place_json['address'] = val.address
            results.append(place_json)
   final_result = {}
   final_result['data'] = results
   final_result["statuscode"] = 0
   final_result["message"] = "success"
   return JsonResponse(final_result,safe=False)