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

如何解决Odoo 12 在尝试从记录中访问模型记录时出现 TypeError

我正在尝试制作一个简单的控制器,它使用 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:\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,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,line 1490,in dispatch
    return response(environ,start_response)
TypeError: 'product.template' object is not callable - - -

解决方法

您需要返回 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 
,

您可以将 route 类型更改为 json 并返回一个 dict 或一个 JSON 对象。

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

class Controller(http.Controller):

    @http.route('/test',type='json',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","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}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res