Skip to main content
 首页 » 编程设计

python之将 bool 字段从父类更改为子类之Odoo v8

2024年10月01日8mate10pro

考虑一下:

@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,并将 bool 字段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字段在子类One2many order_lines字段上:

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_binding和is_edging。

无论如何,知道一个就足以弄清楚另外两个(我猜,哈哈)所以,由于 state 在父类上,它工作得很好,记录的状态实际上发生了变化,然而,虽然 is_book_block 没有语法错误,但它应该更改为 True,但事实并非如此,所以,我认为这是因为该方法在 上循环>order_lines 但它仅更改父类 (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')