将布尔字段从父 class 更改为子 class - Odoo v8

Change boolean field from parent class to child class - Odoo v8

考虑一下:

@api.multi
@api.onchange('order_lines', 'order_lines.is_book_block', '')
@api.constrains('order_lines', 'order_lines.isbn')
def check_quantity(self):
    location = self.printer_book_block.property_stock_supplier.id
    for rec in self:
        if rec.order_lines:
            for line in rec.order_lines:
                if line.qty > line.isbn.with_context({ 'location': location, }).qty_available >= 0:#line.isbn.qty_available in location:
                    rec.write({'state': 'awaitingraw'})
                else:
                    rec.write({'state': 'work_in_progress', 'is_book_block': True})

当指定位置有足够的产品数量(isbn)时,将文档状态更改为work_in_progess,并将布尔字段is_book_block更改为True

state 字段在父模型上:

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')
order_lines = fields.One2many('bsi.print.order.lines', 'print_order', string="Order lines")
book_block = fields.Boolean(string="Book Block", default=True)
binding = fields.Boolean(string="Binding")
edging = fields.Boolean(string="Edging")
state = fields.Selection([
        ('draft','Draft'),
        ('awaitingraw','Awaiting raw materials'),
        ('work_in_progress','Print in Progress'),
        ('delivered','Delivered'),
        ('cancel','Cancel'),
    ], string="State")

is_book_block 字段在子 class One2many order_lines field:

class bsi_print_order_lines(models.Model):
    _name = 'bsi.print.order.lines'

    print_order = fields.Many2one('bsi.print.order', string="Print 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")
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")

还有另外两个字段is_bindingis_edging

无论如何,知道一个就足以弄清楚另外两个(我猜,哈哈)所以,由于 state 在父 class 上,它工作得很好,记录的状态然而,实际上发生了变化,虽然 is_book_block 没有语法错误,但它应该更改为 True,但它没有,所以,我认为这是因为该方法在 order_lines 上循环但它只改变父 class 的东西 (bsi.production.order).

有什么想法吗?

根据我的理解,is_book_block在你遍历行时在行中,每一行都是一条记录,如果你想设置多个字段或只是更改该字段,你可以调用 write:

  # when you change just one field no need for write just 
  rec.state = 'work_in_progress' # is enough
  # because rec.state will trigger write method
  # but for more than one field use write to trigger it only one time
  # line is a record too you can call write also if you want
  line.is_book_block = True

一件事从 onchange 装饰器中删除 order_lines.is_book_block 因为你将触发对该方法的不定式调用:

  @api.onchange('order_lines')