python - 避免从计算字段更改类的状态 - Odoo v8

标签 python python-2.7 odoo-8 odoo

我有这些类(class):

class bsi_production_order(models.Model):
    _name = 'bsi.production.order'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    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'

    name = fields.Char('Reference', required=True, index=True, copy=False, readonly='True', default='New')
    order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")
    state = fields.Selection([
        ('inprogress','Draft'),
        ('awaitingraw','Awaiting raw materials'),
        ('work_in_progress','Work in Progress'),
        ('delivered','Delivered'),
        ('cancel','Cancel'),
    ], string="State")
    notes = fields.Text(string="Notes")

class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func") #

    @api.onchange('qty', 'consumed_qty')
    def _remaining_func(self):
        if self.qty or self.consumed_qty:
            self.remaining_qty = self.qty +(-self.consumed_qty)

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")
    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")
    isbns = fields.Many2one('worksheets.isbns', string="Worksheet ISBNS")

@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
    if self.consumed_qty or self.qty:
        self.remaining_qty = self.qty +(-self.consumed_qty)

我使用以下方法从 bsi.product.order 类创建一个 bsi.print.order:

@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,
                'qty': rec.qty,
                }
            ))
        copy_record.create({
            'state' : 'inprogress', 
            'order_lines': order_lines, # here we pass the list of commands that we created earlier
        })
        record.update({'state': 'print_order_inprogress',},)

但无论我选择什么状态,在 record.update 上,它的状态总是会在 bsi.print.order 上更改。

我知道这是因为计算字段的原因,那么有没有办法间接传递它们?或者一种不影响子类的状态的方法? (bsi.print.order)。

编辑

这些是计算字段:

class bsi_production_order_lines(models.Model):
    _name = 'bsi.production.order.lines'

    production_order = fields.Many2one('bsi.production.order', string="Production Orders")
    isbn = fields.Many2one('product.product', string="ISBN", domain="[('is_isbn', '=', True)]")
    qty = fields.Float(string="Quantity")
    consumed_qty = fields.Float(string="Consumed quantity")
    remaining_qty = fields.Float(string="Remaining quantity", compute="_remaining_func") #

@api.onchange('qty', 'consumed_qty')
def _remaining_func(self):
    for s in self:
        for qty in s.isbn:
            if s.qty or s.consumed_qty:
                s.remaining_qty = s.qty +(-s.consumed_qty)

在另一个类上,这些是相同的字段。

最佳答案

尝试在 ORM create 方法中将 update 替换为 write 。我认为这是唯一的问题,因为我在任何地方都看不到计算字段,那么为什么在从 create 方法修改值字段后它会发生变化?我认为在 ORM 方法中使用 update 是行不通的,所以状态永远不会改变它的值。

@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,
                'qty': rec.qty,
                }
            ))
        copy_record.create({
            'state' : 'inprogress', 
            'order_lines': order_lines, # here we pass the list of commands that we created earlier
        })
        record.write({'state': 'print_order_inprogress',},)

关于python - 避免从计算字段更改类的状态 - Odoo v8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47298813/

相关文章:

python - 如何修复重叠的注释/文本

python - 是否可以在 python (2.4) 中制作这种装饰器?

Python2.6无法导入urandom

python - 垃圾收集如何处理多个正在运行的进程/线程?

python - 类型错误 : appliquer() takes at least 4 arguments (2 given)

javascript - 如何管理 QWEB 报告中的空间

python - '导入错误 : No module named pytz' when trying to import pylab?

接收和发出返回值的 Python 协程

python - 如何在 python 2.7 中将字符串转换为字节串

odoo - 在计算字段上搜索时请求超时