Odoo 12在试图从记录访问模型记录时获得TypeError on



我正在尝试使用Python在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:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugserving.py", line 205, in run_wsgi
execute(self.server.app)
File "C:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugserving.py", line 193, in execute
application_iter = app(environ, start_response)
File "C:UsersSofianeDesktopOdoo12serverodooserviceserver.py", line 342, in app
return self.app(e, s)
File "C:UsersSofianeDesktopOdoo12serverodooservicewsgi_server.py", line 128, in application
return application_unproxied(environ, start_response)
File "C:UsersSofianeDesktopOdoo12serverodooservicewsgi_server.py", line 117, in application_unproxied
result = odoo.http.root(environ, start_response)
File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1317, in __call__
return self.dispatch(environ, start_response)
File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1290, in __call__
return self.app(environ, start_wrapped)
File "C:UsersSofianeDesktopOdoo12pythonlibsite-packageswerkzeugwsgi.py", line 599, in __call__
return self.app(environ, start_response)
File "C:UsersSofianeDesktopOdoo12serverodoohttp.py", line 1490, in dispatch
return response(environ, start_response)
TypeError: 'product.template' object is not callable - - -

您可以将路由类型更改为json,并返回字典或JSON对象。

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

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) {

});

在演示数据库中返回值的示例:

{"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}}

你需要返回HTML响应

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 

相关内容

最新更新