【发布时间】:2016-09-21 08:52:52
【问题描述】:
环境
我创建了两个TransientModel(命名为lots.manager 和product.lot.available),它们之间的关系为One2many(一个批次经理将有多个可用批次)。
我的目的是显示可用批次的列表,用户将能够选择他想要使用的批次以及每个批次的数量。所以product.lot.available 有字段lot_id(选择批次)、selected(Boolean,表示是否使用批次)和qty(Float,表示使用的数量每个批次)。
另一方面,在lots.manager 模型中,我有一个名为total_qty_selected 的计算字段,它计算selected 字段为True的所有可用批次的qty 的总和em>。
代码
class LotsManager(models.TransientModel):
_name = 'lots.manager'
@api.multi
@api.depends('product_lots_available', 'product_lots_available.selected',
'product_lots_available.qty')
def _compute_total_qty_selected(self):
for manager in self:
total = 0
for lot in manager.product_lots_available:
if lot.selected is True:
total += lot.qty
manager.total_qty_selected = total
move_id = fields.Many2one(
comodel_name='stock.move',
string='Stock move',
required=True,
select=True,
readonly=True,
)
product_id = fields.Many2one(
comodel_name='product.product',
related='move_id.product_id',
string='Product',
)
product_lots_available = fields.One2many(
comodel_name='product.lot.available',
inverse_name='manager_id',
string='Available lots',
)
total_qty_selected = fields.Float(
compute='_compute_total_qty_selected',
string='Total quantity selected',
)
class ProductLotAvailable(models.TransientModel):
_name = 'product.lot.available'
manager_id = fields.Many2one(
comodel_name='lots.manager',
string='Lots Manager',
)
lot_id = fields.Many2one(
comodel_name='stock.production.lot',
string='Lot',
readonly=True,
)
selected = fields.Boolean(
string='Selected',
default=False,
)
qty = fields.Float(
string='Quantity',
default=0.00,
)
@api.onchange('selected')
def onchange_selected(self):
if self.selected is True:
_logger.info(self.manager_id.product_id.name)
_logger.info(self.manager_id.total_qty_selected)
问题
计算域 total_qty_selected 计算得很好(我在视图中显示它并且效果很好),但是,当我尝试从 product.lot.available 读取它时,我总是得到 0。例如,_logger 中的行上面的onchange 函数,正确显示产品名称,但total_qty_selected 返回 0,而不是在那一刻我可以读取表单中的 2.00,或者任何不同于 0 的值。
我需要在 onchange 函数中获得正确的值来做一些事情。
谁能帮我解决这个问题?
【问题讨论】:
标签: python-2.7 openerp odoo-8