将父模型中的布尔字段检查为行 - Odoo v8
Check boolean field from parent model into lines - Odoo v8
我想检查一个布尔字段,从父项 class 到子项(行)class。
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",default=fields.Datetime.now)
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
due_date = fields.Date(string="Due Date")
journal_entry = fields.Many2one('account.move', string="Journal Entry")
stock_picking_id = fields.Many2one('stock.picking', string="Stock Picking")
order_picking = 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")
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") #, 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")
所以,当book_block
按钮被选中时,is_book_block
也应该被自动选中,与binding
和edging
一样,他们应该检查is_binding
和 is_edging
分别。
这些是我有的方法:
@api.depends('book_block', 'order_picking', 'order_picking.is_book_block')
def _bool_book_block(self):
if self.book_block == True:
order_picking.is_book_block = True
else:
order_picking.is_book_block = False
@api.depends('binding', 'order_picking', 'order_picking.is_binding')
def _bool_binding(self):
if self.binding == True:
order_picking.is_binding = True
else:
order_picking.is_binding = False
@api.onchange('edging', 'order_picking', 'order_picking.is_edging')
def _bool_edging(self):
if self.edging == True:
order_picking.is_edging == True
else:
order_picking.is_edging == False
但它不起作用,例如我确实检查了 edging
,甚至保存了文档,但行中的布尔字段没有变化。
有什么想法吗?
当您使用 depends 字段时,该字段必须具有计算属性:
some_feild = fiels.SomeType(......., compute='method_name')
@api.depends('some_field_2', ...)
def method_name(self):
# and here the function will be computed.
如果你的字段是 100% 的计算字段,那么你在 odoo 中最好的东西
可以侦听父 class 中的变化以计算子字段:
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
....
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
is_book_block = fields.Boolean(string="Is Book Block Done", compute="compute_book_block")
@api.depends('print_order.book_block')
def compute_book_block(self):
# in depends it's always a good this to loop
# or use api.one with depends
for rec in self:
if rec.print_order:
self.is_book_block = self.print_order.book_block
但只有当该字段始终是计算字段时才可以,我的意思是用户永远不会
从 UI.
更改它的值
如果您的字段未被计算并且用户可以根据需要更改值而不是您需要使用
onchange 在父 class.
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
....
@api.onchange('book_block '):
def onchange_book_block(self):
# in onchange no need for loop
# because self should always have only one recod
for line in self.order_picking:
line.is_book_block = self.book_block
我想检查一个布尔字段,从父项 class 到子项(行)class。
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",default=fields.Datetime.now)
production_orders = fields.Many2one('bsi.production.order', ondelete='cascade', string="Production Order")
due_date = fields.Date(string="Due Date")
journal_entry = fields.Many2one('account.move', string="Journal Entry")
stock_picking_id = fields.Many2one('stock.picking', string="Stock Picking")
order_picking = 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")
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") #, 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")
所以,当book_block
按钮被选中时,is_book_block
也应该被自动选中,与binding
和edging
一样,他们应该检查is_binding
和 is_edging
分别。
这些是我有的方法:
@api.depends('book_block', 'order_picking', 'order_picking.is_book_block')
def _bool_book_block(self):
if self.book_block == True:
order_picking.is_book_block = True
else:
order_picking.is_book_block = False
@api.depends('binding', 'order_picking', 'order_picking.is_binding')
def _bool_binding(self):
if self.binding == True:
order_picking.is_binding = True
else:
order_picking.is_binding = False
@api.onchange('edging', 'order_picking', 'order_picking.is_edging')
def _bool_edging(self):
if self.edging == True:
order_picking.is_edging == True
else:
order_picking.is_edging == False
但它不起作用,例如我确实检查了 edging
,甚至保存了文档,但行中的布尔字段没有变化。
有什么想法吗?
当您使用 depends 字段时,该字段必须具有计算属性:
some_feild = fiels.SomeType(......., compute='method_name')
@api.depends('some_field_2', ...)
def method_name(self):
# and here the function will be computed.
如果你的字段是 100% 的计算字段,那么你在 odoo 中最好的东西 可以侦听父 class 中的变化以计算子字段:
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
....
class bsi_print_order_lines(models.Model):
_name = 'bsi.print.order.lines'
is_book_block = fields.Boolean(string="Is Book Block Done", compute="compute_book_block")
@api.depends('print_order.book_block')
def compute_book_block(self):
# in depends it's always a good this to loop
# or use api.one with depends
for rec in self:
if rec.print_order:
self.is_book_block = self.print_order.book_block
但只有当该字段始终是计算字段时才可以,我的意思是用户永远不会 从 UI.
更改它的值如果您的字段未被计算并且用户可以根据需要更改值而不是您需要使用 onchange 在父 class.
class bsi_print_order(models.Model):
_name = 'bsi.print.order'
....
@api.onchange('book_block '):
def onchange_book_block(self):
# in onchange no need for loop
# because self should always have only one recod
for line in self.order_picking:
line.is_book_block = self.book_block