Skip to Content
Menu
This question has been flagged
3110 Views

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)
Avatar
Discard

see this link :

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

Related Posts Replies Views Activity
1
May 21
6641
0
Jan 21
11391
2
Dec 19
8031
1
Apr 17
10831
2
May 15
5951