首页 文章

为什么无法从Odoo8中的TransientModel访问计算字段?

提问于
浏览
0

ENVIRONMENT

我创建了两个 TransientModel (名为 lots.managerproduct.lot.available ),它们之间有一个 One2many 关系(很多经理会有几个可用的批次) .

我的目的是显示可用批次的列表,用户将能够选择他想要使用的批次以及每个批次的数量 . 所以 product.lot.available 的字段为 lot_id (选择一个批次), selected (表示是否使用该批次的 Boolean )和 qty (表示每批次使用的数量的 Float ) .

另一方面,在 lots.manager 模型中,我有一个名为 total_qty_selected 的计算字段,它计算 selected 字段为True的所有可用批次的 qty 之和 .

CODE

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)

PROBLEM

计算字段 total_qty_selected 计算得很好(我在视图中显示并且效果很好),但是,当我尝试从 product.lot.available 读取它时,我总是得到0.例如,上面的 onchange 函数中的 _logger 行,显示名称产品权利,但 total_qty_selected 返回0,而不是在那一刻我可以在表格中读取2.00,或不同于0的任何值 .

我需要在onchange函数中获得正确的值来做一些事情 .

任何人都可以帮我如何管理这个?

2 回答

  • 0

    在计算字段中使用 store=True . 试试下面的代码

    class LotsManager(models.TransientModel):
    _name = 'lots.manager'
    
       total_qty_selected = fields.Float(
        compute='_compute_total_qty_selected',
        string='Total quantity selected', store=True
    )
    
  • 0

    最后,我设法用一个可怕的解决方法解决了这个问题 . 但它运作良好 .

    我将以下字段添加到 lots.manager 模型:

    total_qty_selected_copy = fields.Float(
        string='Total quantity selected (copy)',
        default=False,
    )
    

    每次原始字段都会更改此字段,添加以下代码:

    @api.one
    @api.onchange('total_qty_selected')
    def onchange_selected(self):
        self.total_qty_selected_copy = self.total_qty_selected
    

    显然,我必须将 total_qty_selected_copy 添加到XML视图中,并使其不可见 .

    在这些修改之后,我可以通过这个新字段从 product.lot.available 模型获得我需要的值:

    @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_copy)
    

相关问题