在同一方法上传递对象字段和 one2many 字段 - Odoo v8
Pass object fields and one2many fields on same method - Odoo v8
我有这个方法:
@api.multi
def create_print(self,vals):
rec_production_order = self.env['bsi.production.order'].search([], order='id desc', limit=1)
self.env['bsi.print.order'].create({
'origin': rec_production_order.name,
'state': 'draft',
})
if order_lines in rec_production_order:
vals.update({'order_lines':[(0,0,
{
'isbn':'isbn',
'qty':'consumed_qty',
})]})
return super(bsi_print_order,self).create(vals)
此方法在 'bsi.production.order'
对象上,这些是我正在使用的对象:
class bsi_production_order(models.Model):
_name = 'bsi.production.order'
_inherit = ['product.product']
@api.model
def create(self, vals):
if vals.get('name', 'New') == 'New':
if vals.get('production_type') == 'budgeted':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
elif vals.get('production_type') == 'nonbudgeted':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
elif vals.get('production_type') == 'direct':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'
return super(bsi_production_order, self).create(vals)
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")
origin = fields.Char(string="Origin")
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")
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
print_order = fields.Many2one('bsi.print.order', string="Print Order")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Integer(string="Quantity")
consumed_qty = fields.Integer(string="Quantity consumed")
remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
state = fields.Selection([
('draft','Draft'),
('inprogress','In progress'),
('readytomove','Ready to move'),
('intransit','In transitt'),
('done','Done'),
], string="State")
is_book_block = fields.Boolean(string="Is Book Block Done")
is_binding = fields.Boolean(string="Is Binding Done")
is_edging = fields.Boolean(string="Is Edging Done")
@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
if self.consumed_qty or self.qty:
self.remaining_qty = self.consumed_qty - self.qty
我想将 bsi.production.order
中当前表单的记录传递给 bsi.print.order
和 bsi.print.order.lines
,每次我单击上述方法时它都会抛出我:
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\bsi\models\models.py", line 381, in create_print
NameError: global name 'order_lines' is not defined
有什么想法吗?
首先,如果您从按钮调用此方法,则无需获取任何记录
因为 self (RecordSet) 将只包含该记录,但它更好
如果您想在使用 odoo-API.
中调用此方法,则可以以任何方式循环
def @api.multi
def create_print(self,vals):
# first define the empty model to call create from
copy_record = self.env['other.model'] # now you call the method directly
for record in self:
# like this is safer
# here create a list of cammands for you new o2m_field
o2m_field = []
for rec in record.o2m_field:
# here loop through all o2m record to create the list of commands
o2m_field.append(
(0,0,
{
'some_field': rec.some_field,
'some_field2': rec.some_field2,
'some_m2o_field': rec.some_m2o_field.id,# m2o pass interger value
# hope you don't have o2m_field here too. or you need to use imagination to create
# a good method that creates this list of commands.
}
)
# now create the record
copy_record.create(
{
'simple_field' : record.simple_field, # for simple field type char,date,...
'm2o_field': record.m2o_field.id, # pass the id always
'o2m_field': o2m_field, # here we pass the list of commands that we created earlier
})
我想你明白复制记录是一个非常复杂的操作,特别是当复制中也有 m2m 和 o2m 字段时。
我有这个方法:
@api.multi
def create_print(self,vals):
rec_production_order = self.env['bsi.production.order'].search([], order='id desc', limit=1)
self.env['bsi.print.order'].create({
'origin': rec_production_order.name,
'state': 'draft',
})
if order_lines in rec_production_order:
vals.update({'order_lines':[(0,0,
{
'isbn':'isbn',
'qty':'consumed_qty',
})]})
return super(bsi_print_order,self).create(vals)
此方法在 'bsi.production.order'
对象上,这些是我正在使用的对象:
class bsi_production_order(models.Model):
_name = 'bsi.production.order'
_inherit = ['product.product']
@api.model
def create(self, vals):
if vals.get('name', 'New') == 'New':
if vals.get('production_type') == 'budgeted':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.budgeted') or '/'
elif vals.get('production_type') == 'nonbudgeted':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.non_budgeted') or '/'
elif vals.get('production_type') == 'direct':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.production.direct') or '/'
return super(bsi_production_order, self).create(vals)
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")
origin = fields.Char(string="Origin")
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")
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
print_order = fields.Many2one('bsi.print.order', string="Print Order")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
qty = fields.Integer(string="Quantity")
consumed_qty = fields.Integer(string="Quantity consumed")
remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func")
state = fields.Selection([
('draft','Draft'),
('inprogress','In progress'),
('readytomove','Ready to move'),
('intransit','In transitt'),
('done','Done'),
], string="State")
is_book_block = fields.Boolean(string="Is Book Block Done")
is_binding = fields.Boolean(string="Is Binding Done")
is_edging = fields.Boolean(string="Is Edging Done")
@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
if self.consumed_qty or self.qty:
self.remaining_qty = self.consumed_qty - self.qty
我想将 bsi.production.order
中当前表单的记录传递给 bsi.print.order
和 bsi.print.order.lines
,每次我单击上述方法时它都会抛出我:
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\bsi\models\models.py", line 381, in create_print
NameError: global name 'order_lines' is not defined
有什么想法吗?
首先,如果您从按钮调用此方法,则无需获取任何记录 因为 self (RecordSet) 将只包含该记录,但它更好 如果您想在使用 odoo-API.
中调用此方法,则可以以任何方式循环def @api.multi
def create_print(self,vals):
# first define the empty model to call create from
copy_record = self.env['other.model'] # now you call the method directly
for record in self:
# like this is safer
# here create a list of cammands for you new o2m_field
o2m_field = []
for rec in record.o2m_field:
# here loop through all o2m record to create the list of commands
o2m_field.append(
(0,0,
{
'some_field': rec.some_field,
'some_field2': rec.some_field2,
'some_m2o_field': rec.some_m2o_field.id,# m2o pass interger value
# hope you don't have o2m_field here too. or you need to use imagination to create
# a good method that creates this list of commands.
}
)
# now create the record
copy_record.create(
{
'simple_field' : record.simple_field, # for simple field type char,date,...
'm2o_field': record.m2o_field.id, # pass the id always
'o2m_field': o2m_field, # here we pass the list of commands that we created earlier
})
我想你明白复制记录是一个非常复杂的操作,特别是当复制中也有 m2m 和 o2m 字段时。