跳至内容
菜单
此问题已终结
3760 查看

hello i have code in my controller for return response like this

from odoo import http

from odoo.http import request

import json

import time

import datetime

from datetime import timedelta

from odoo.http import Controller,route,JsonRequest, Response

class list_code_tes(http.Controller):

    @http.route('/api/code/tes', type='json', auth='user')

    def list_code_tes(self, **rec):

        if request.jsonrequest:

            surgery = request.env['oeh.medical.surgery']

            

            medical_record = rec['medicalrecord']

            

            patient = request.env['oeh.medical.patient'].search([('identification_code','=', medical_record )], limit=1)

            if not patient :

                return ({

                    'code': 404,

                    'status': 'NOT FOUND',

                    'message': 'Patient not found.'

                })

            

            search_surgery = request.env['oeh.medical.surgery'].search([('patient','=', patient.id)],limit=1)

            if not search_surgery:

                return ({

                    'code': 404,

                    'status': 'NOT FOUND',

                    'message': 'Surgery Not Found.'

                })

            if search_surgery:

                code = search_surgery.booking_code

                date= search_surgery.date

                name = search_surgery.unit_id.name

                a = {

                    "bookingcode": code,

                    "surgerydate": date,

                    "poli": name,

                    "lastupdate": search_surgery.write_date,

                    

                }

            return a

when using this code the return response will be like this:

"response": {
        "lastupdate""2020-11-27 13:12:23",
        "bookingcode""S/20112700005",
        "poli""test",
        "surgerydate""2020-11-27"
    } but i need the return the response like this

"response": {
    "list" :[{
        "lastupdate""2020-11-27 13:12:23",
        "bookingcode""S/20112700005",
        "poli""test",
        "surgerydate""2020-11-27"
    },
{
        "lastupdate""2020-11-27 13:12:23",
        "bookingcode""S/20112700006",
        "poli""test123",
        "surgerydate""2020-11-27"    
]}
}

can someone help me how to change the response? (*note : i use limit=1 in search surgery because i dont know how to make response looping like i want)
形象
丢弃

see this link :

https://stackoverflow.com/questions/2733813/iterating-through-a-json-object

相关帖文 回复 查看 活动
1
7月 25
8603
0
1月 21
13265
2
12月 19
9679
1
4月 17
12834
2
5月 15
7189