gpt4 book ai didi

python - 从另一个模型创建模型 - 使用 Many2one 字段 - Odoo v8

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:35 24 4
gpt4 key购买 nike

考虑这两个模型:

class bsi_production_order(models.Model):
_name = 'bsi.production.order'
_inherit = ['product.product']

name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Production Date")
notes = fields.Text(string="Notes")
order_lines = fields.One2many('bsi.production.order.lines', 'production_order', states={'finished': [('readonly', True)], 'cancel': [('readonly', True)]}, string="Order lines", copy=True)
print_orders = fields.One2many('bsi.print.order', 'production_orders', string="Print Orders")

class bsi_print_order(models.Model):
_name = 'bsi.print.order'
_inherit = ['mail.thread','mrp.worksheet.contract']

name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Print Date")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
due_date = fields.Date(string="Due Date")
state = fields.Selection([
('draft','Draft'),
('awaitingraw','Awaiting raw materials'),
('wip','Work in Progress'),
('delivered','Delivered'),
('cancel','Cancel'),
], string="State")
notes = fields.Text(string="Notes")

我想创建一条从 bsi.production.orderbsi.print.order 的新记录,它有两个字段,stateproduction_order,后者是 bsi.production.ordername 字段。

可以看到bsi.print.order上的这个字段是一个Many2one字段,而在bsi.production.order上是一个Char,这是我到目前为止尝试过的:

@api.multi
def create_print(self):
self.env['bsi.print.order'].create({
'production_orders': 'name',
'state': 'draft',
})

如果我将 state 留在字典中,它会正确创建,但如果我这样尝试,它会抛出我:

Traceback (most recent call last):
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 546, in _handle_exception
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 583, in dispatch
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 319, in _call_function
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\service\model.py", line 118, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 316, in checked_call
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 812, in __call__
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\http.py", line 412, in response_wrap
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 948, in call_button
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\web\controllers\main.py", line 936, in _call_kw
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 268, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 399, in old_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\mrp_worksheet_contract\models\models.py", line 262, in create_print
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 266, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\mrp_worksheet_contract\models\models.py", line 466, in create
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 266, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\mrp_worksheet_contract\models\models.py", line 20, in create
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 266, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 508, in new_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\openerp\addons\mail\mail_thread.py", line 381, in create
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 268, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 372, in old_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\models.py", line 4101, in create
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 266, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\api.py", line 508, in new_api
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\models.py", line 4245, in _create
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\sql_db.py", line 171, in wrapper
File "C:\Program Files (x86)\Odoo 8.0-20170914\server\.\openerp\sql_db.py", line 247, in execute
DataError: la sintaxis de entrada no es válida para integer: «name»
LINE 1: ...ES(nextval('bsi_print_order_id_seq'), 'PO-00009', 'name', fa...

所以,由于这是 bsi.print.order 中的 Many2one,我做错了什么?

编辑

似乎不是将 bsi.production.order name 字段传递给 production_orders Many2one 字段,它实际上是尝试用它自己的创建 bsi.print.order 名称 字段。

最佳答案

在Odoo中,Many2one字段存储整数值/目标表的ID。

在您的情况下,您正试图传递导致错误的 char 值。

尝试使用以下代码:

@api.multi
def create_print(self):
# You need to pass ID as per your requirement
rec_production_order = self.env['bsi.production.order'].browse(1)
self.env['bsi.print.order'].create({
'production_orders': rec_production_order.id,
'name': rec_production_order.name
'state': 'draft',
})

关于python - 从另一个模型创建模型 - 使用 Many2one 字段 - Odoo v8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46658572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com