Odoo 12 在尝试从记录访问模型记录时出现 TypeError

Odoo 12 getting TypeError on while trying to access models record from record

我正在尝试使用 Python 制作一个简单的控制器 returns Odoo 12 中的 json 对象。我一直在学习几个教程,但每次尝试使用 env['product.template'] 时,我总是遇到同样的错误。这是我的控制器代码。

import odoo.http as http
from odoo import SUPERUSER_ID
from odoo import registry as registry_get
from odoo.api import Environment
import json


class Controller(http.Controller):

    @http.route('/test', type='http', auth='public', website=False)
    def handler(self):
        registry = registry_get('ceres')
        with registry.cursor() as cr:
            env = Environment(cr, SUPERUSER_ID, {})
            attendee = env['product.template'].sudo().search([])
        return attendee 

这是我在调用控制器时收到的异常:

2021-01-25 08:43:08,686 5912 ERROR ceres werkzeug: Error on request:
Traceback (most recent call last):
  File "C:\Users\Sofiane\Desktop\Odoo12\python\lib\site-packages\werkzeug\serving.py", line 205, in run_wsgi
    execute(self.server.app)
  File "C:\Users\Sofiane\Desktop\Odoo12\python\lib\site-packages\werkzeug\serving.py", line 193, in execute
    application_iter = app(environ, start_response)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\service\server.py", line 342, in app
    return self.app(e, s)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\service\wsgi_server.py", line 128, in application
    return application_unproxied(environ, start_response)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\service\wsgi_server.py", line 117, in application_unproxied
    result = odoo.http.root(environ, start_response)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\http.py", line 1317, in __call__
    return self.dispatch(environ, start_response)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\http.py", line 1290, in __call__
    return self.app(environ, start_wrapped)
  File "C:\Users\Sofiane\Desktop\Odoo12\python\lib\site-packages\werkzeug\wsgi.py", line 599, in __call__
    return self.app(environ, start_response)
  File "C:\Users\Sofiane\Desktop\Odoo12\server\odoo\http.py", line 1490, in dispatch
    return response(environ, start_response)
TypeError: 'product.template' object is not callable - - -

您需要returnhtml回应

from odoo.http import request
import odoo.http as http
from odoo import SUPERUSER_ID
from odoo import registry as registry_get
from odoo.api import Environment
import json


class Controller(http.Controller):

    @http.route('/test', type='http', auth='public', website=False)
    def handler(self):
        registry = registry_get('ceres')
        with registry.cursor() as cr:
            env = Environment(cr, SUPERUSER_ID, {})
            attendee = env['product.template'].sudo().search([])
        response = request.make_response(attendee)
        return response 

您可以将 route 类型更改为 json 和 return 字典或 JSON 对象。

在下面的示例中,我们 return 每个产品的 namelist_price 使用 json 对象:

class Controller(http.Controller):

    @http.route('/test', type='json', auth='public', website=False)
    def handler(self):
        records = request.env['product.template'].sudo().search_read([], fields=['name', 'list_price'])
        return json.dumps({r['id']: r for r in records}) 

你可以用web.ajax来调用它:

var ajax = require('web.ajax');
ajax.jsonRpc('/test', 'call', {}).then(function (data) {
    
});

演示数据库中 returned 值的示例:

{"23": {"id": 23, "name": "Acoustic Bloc Screens", "list_price": 2950.0}, "15": {"id": 15, "name": "Cabinet with Doors", "list_price": 14.0}, "29": {"id": 29, "name": "Chair floor protection", "list_price": 12.0}, "16": {"id": 16, "name": "Conference Chair", "list_price": 16.5}, "18": {"id": 18, "name": "Corner Desk Black", "list_price": 85.0}, "10": {"id": 10, "name": "Corner Desk Right Sit", "list_price": 147.0}, "9": {"id": 9, "name": "Customizable Desk", "list_price": 750.0}, "28": {"id": 28, "name": "Deposit", "list_price": 150.0}, "8": {"id": 8, "name": "Desk Combination", "list_price": 450.0}, "21": {"id": 21, "name": "Desk Stand with Screen", "list_price": 2100.0}, "24": {"id": 24, "name": "Drawer", "list_price": 3645.0}, "19": {"id": 19, "name": "Drawer Black", "list_price": 25.0}, "20": {"id": 20, "name": "Flipover", "list_price": 1950.0}, "25": {"id": 25, "name": "Four Person Desk", "list_price": 23500.0}, "2": {"id": 2, "name": "Hotel Accommodation", "list_price": 400.0}, "22": {"id": 22, "name": "Individual Workplace", "list_price": 885.0}, "11": {"id": 11, "name": "Large Cabinet", "list_price": 320.0}, "13": {"id": 13, "name": "Large Desk", "list_price": 1799.0}, "26": {"id": 26, "name": "Large Meeting Table", "list_price": 40000.0}, "5": {"id": 5, "name": "Office Chair", "list_price": 70.0}, "17": {"id": 17, "name": "Office Chair Black", "list_price": 12.5}, "7": {"id": 7, "name": "Office Design Software", "list_price": 280.0}, "6": {"id": 6, "name": "Office Lamp", "list_price": 40.0}, "14": {"id": 14, "name": "Pedal Bin", "list_price": 47.0}, "1": {"id": 1, "name": "Restaurant Expenses", "list_price": 14.0}, "12": {"id": 12, "name": "Storage Box", "list_price": 79.0}, "27": {"id": 27, "name": "Three-Seat Sofa", "list_price": 60000.0}, "4": {"id": 4, "name": "Virtual Home Staging", "list_price": 38.25}, "3": {"id": 3, "name": "Virtual Interior Design", "list_price": 30.75}}