从 POST 请求向模型写入数据 (Odoo 9.0)
Writing data to model from POST request (Odoo 9.0)
我有以下型号:
class LibraryBook(models.Model):
_name = 'library.book'
name = fields.Char('Title', required=True)
date_release = fields.Date("Release Date")
author_ids = fields.Many2many("res.partner", string="Authors")
我是 Odoo 的新手,正在尝试了解如何从 POST 请求中将数据保存到我的模型的基础知识,如下所示
curl -i -X POST --data "name=Odoo%20-%20Much%20Mystery,%20Wow&author_id=Doge" http://0.0.0.0:8069/test
我找到了一种方法,将控制器中的 csrf
参数设置为 false
,如下所示:
[...]
@http.route('/test', type='http', auth='public',methods=['POST'], website=True, csrf=False)
def test(self, **kwargs):
record = request.env['library.book'].sudo()
record.create(kwargs)
我现在想知道是否有一种方法可以避免设置 csrf=false
,因为我读到过这样做通常是个坏主意。另外,我需要什么才能摆脱 .sudo()
?不设置 csrf=false
会导致 400 BAD REQUEST
和 Invalid CSRF token
。删除 sudo()
会导致 500 INTERNAL SERVER ERROR
。在 Odoo Development Cookbook 中,它在一个示例中说 auth='none'
Lack of a user is also why we have to sudo() all our calls to model methods in the example code
假设我期望来自 API 的 POST 请求,是否可以将其与用户关联,这样我就不必 sudo()
?
如果能对此作出任何澄清,我将不胜感激。
更新
所以我刚找到 this(第 817 行):
- if the form is accessed by an external third party (e.g. REST API endpoint, payment gateway callback) you will need to disable CSRF
protection (and implement your own protection if necessary) by
passing the csrf=False
parameter to the route
decorator.
我想这只剩下一个问题悬而未决,关于 sudo
。
SUDO()
creates a new environment with the provided user set, uses the administrator if none is provided (to bypass access rights/rules in safe contexts), returns a copy of the recordset it is called on using the new environment:
Odoo 不允许 public 用户创建、更新、删除记录。
如果我们想从 public 用户创建一条记录,那么我们需要使用 sudo() 创建一条记录。
以管理员身份创建记录对象
request.env['library.book'].sudo().create(vals)
希望对您有所帮助。
有关更多信息,您可以导航至以下链接:
https://www.odoo.com/documentation/9.0/reference/orm.html
谢谢
我有以下型号:
class LibraryBook(models.Model):
_name = 'library.book'
name = fields.Char('Title', required=True)
date_release = fields.Date("Release Date")
author_ids = fields.Many2many("res.partner", string="Authors")
我是 Odoo 的新手,正在尝试了解如何从 POST 请求中将数据保存到我的模型的基础知识,如下所示
curl -i -X POST --data "name=Odoo%20-%20Much%20Mystery,%20Wow&author_id=Doge" http://0.0.0.0:8069/test
我找到了一种方法,将控制器中的 csrf
参数设置为 false
,如下所示:
[...]
@http.route('/test', type='http', auth='public',methods=['POST'], website=True, csrf=False)
def test(self, **kwargs):
record = request.env['library.book'].sudo()
record.create(kwargs)
我现在想知道是否有一种方法可以避免设置 csrf=false
,因为我读到过这样做通常是个坏主意。另外,我需要什么才能摆脱 .sudo()
?不设置 csrf=false
会导致 400 BAD REQUEST
和 Invalid CSRF token
。删除 sudo()
会导致 500 INTERNAL SERVER ERROR
。在 Odoo Development Cookbook 中,它在一个示例中说 auth='none'
Lack of a user is also why we have to sudo() all our calls to model methods in the example code
假设我期望来自 API 的 POST 请求,是否可以将其与用户关联,这样我就不必 sudo()
?
如果能对此作出任何澄清,我将不胜感激。
更新
所以我刚找到 this(第 817 行):
- if the form is accessed by an external third party (e.g. REST API endpoint, payment gateway callback) you will need to disable CSRF
protection (and implement your own protection if necessary) by
passing thecsrf=False
parameter to theroute
decorator.
我想这只剩下一个问题悬而未决,关于 sudo
。
SUDO()
creates a new environment with the provided user set, uses the administrator if none is provided (to bypass access rights/rules in safe contexts), returns a copy of the recordset it is called on using the new environment:
Odoo 不允许 public 用户创建、更新、删除记录。 如果我们想从 public 用户创建一条记录,那么我们需要使用 sudo() 创建一条记录。
以管理员身份创建记录对象
request.env['library.book'].sudo().create(vals)
希望对您有所帮助。 有关更多信息,您可以导航至以下链接: https://www.odoo.com/documentation/9.0/reference/orm.html
谢谢