首页 > 解决方案 > 在odoo中制作一个控制器来处理json

问题描述

我是 odoo 的新手,我使用脚手架命令创建了一个模块,如下所示:

"C:\Program Files (x86)\Odoo 11.0\python\python.exe" "C:\Program Files (x86)\Odoo 11.0\server\odoo-bin" 脚手架 api4"C:\Users\Carlos\Desktop\自定义插件”

当我创建这个基本重定向控制器时它工作正常

# - * - coding: utf-8 - * -
from odoo import http
from odoo.http import request
import json
class Api4 (http.Controller):
    @ http.route ('/ api4 / api4 /', auth = 'public', website = True)
    def index (self):
        return request.redirect ('/ web /')

但是当我创建另一个 @ http.route 来接收 json 并能够处理您的数据时,它对我不起作用,而我之前所做的那个停止工作。

    @ http.route ('/ api / json_get_request', auth = 'public', type = 'json', csrf = False)
    def jsontest (self, ** kw):
        return {'attribute': 'test'}

代码是基本的,但我想看看发送任何 json 是否会返回 {'attribute': 'test'} 而是返回:

{
    "jsonrpc": "2.0",
    "id": null,
    "error": {
        "code": 404,
        "message": "404: Not Found",
        "data": {
            "name": "werkzeug.exceptions.NotFound",
            "debug": "Traceback (most recent call last): \ n File \" C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ http.py \ ", line 653, in _handle_exception \ n return super (JsonRequest, self) ._ handle_exception (exception) \ n File \ "C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ http.py \", line 312, in _handle_exception \ n raise pycompat.reraise (type (exception), exception, sys.exc_info () [2]) \ n File \ "C: \\ Program Files (x86) \\ Odoo 11.0 \\ server \\ odoo \\ tools \\ pycompat.py \ ", line 86, in reraise \ n raise value.with_traceback (tb) \ nwerkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. \ n ",
            "message": "404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.",
            "arguments": [],
            "exception_type": "internal_error"
        },
        "http_status": 404
    }
}

错误邮递员

标签: jsoncontrollerpostmanodooodoo-11

解决方案


在您的 odoo-bin 命令中添加-d--db-filter以仅挑选出一个数据库。例如python3 odoo-bin --addons-path addons,mymodules -d newdatabase。据我所知,auth='public'当有多个odoo数据库时,api会引发这种错误。

替代解决方案是您可以将端点与auth='user'. 您需要先获取登录 cookie。更多内容:如何从 android 应用程序连接到 Odoo 数据库


推荐阅读