I have these models:
class bsi_production_order(models.Model):
_name = 'bsi.production.order'
@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")
production_type = fields.Selection([
('budgeted','Budgeted'),
('nonbudgeted','Non Budgeted'),
('direct','Direct Order'),
], string='Type of Order', index=True,
track_visibility='onchange', copy=False,
help=" ")
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")
state = fields.Selection([
('draft','Draft'),
('start','Started'),
('inprogress','In progress'),
('print_order_inprogress','Print Order In Progress'),
('finished','Finished'),
('cancel','Cancel'),
], string='State', index=True,
track_visibility='onchange', copy=False,
help=" ")
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
@api.model
def create(self, vals):
if vals.get('name', 'New') == 'New':
vals['name'] = self.env['ir.sequence'].next_by_code('bsi.print.order') or '/'
return super(bsi_print_order, self).create(vals)
name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
date = fields.Date(string="Print Date")
origin = fields.Char(string="Origin")
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")
book_block = fields.Boolean(string="Book Block")
binding = fields.Boolean(string="Binding")
edging = fields.Boolean(string="Edging")
contract_worksheet = fields.One2many('mrp.worksheet.contract', 'printer_order', string="Worksheet calculation")
state = fields.Selection([
('draft','Draft'),
('awaitingraw','Awaiting raw materials'),
('wip','Work in Progress'),
('delivered','Delivered'),
('cancel','Cancel'),
], string="State")
notes = fields.Text(string="Notes")
And this method, which belongs to bsi.production.order
, it has to create a bsi.print.order
from it:
@api.multi
def create_printy(self):
copy_record = self.env['bsi.print.order']
for record in self:
order_lines = []
for rec in record.order_lines:
order_lines.append(
(0,0,
{
'isbn': rec.isbn.id,
'consumed_qty': rec.qty,
}
))
copy_record.create({
'state' : 'draft',
'order_lines': order_lines,
})
What is the problem? The problem is that, on bsi.production.order
the state from which this function is visible (the button) is inprogress
, now, this state doesn't exist on bsi.print.order
, but, as You can see, on copy_record.create
, I've passed 'state' as 'draft', but it doesn't works, it throws me:
ValidateError
Error while validating constraint
Wrong value for bsi.print.order.state: 'inprogress'
I mean, it's obvious, but not so much, because the bsi.print.order
should be created on draft
state.
What am I doing wrong?
Does this error come while creating the new record or accessing existing record? It's might set 'inprogress' state in existing records (while developing due to various reasons)...
I think the problem is with 'consumed_qty' which is a computed field, seems like You cannot pass values directly from computed fields, any ideas on that?