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

时间:2017-10-10 04:21:39

标签: python openerp odoo-8

考虑这两个模型:

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,后者为name } bsi.production.order的字段。

您可以在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... 留在dict中,它会正确创建,但如果我尝试这样,它会抛出我:

Many2one

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

修改

似乎不是将bsi.production.order production_orders字段传递到name Many2one字段,而是实际上尝试使用它创建bsi.print.order {{1} 1}} field。

1 个答案:

答案 0 :(得分:2)

在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',
    })