【问题标题】:Why a computed field is not accesible from a TransientModel in Odoo8?为什么无法从 Odoo 8 中的瞬态模型访问计算域?
【发布时间】:2016-09-21 08:52:52
【问题描述】:

环境

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

我的目的是显示可用批次的列表,用户将能够选择他想要使用的批次以及每个批次的数量。所以product.lot.available 有字段lot_id(选择批次)、selectedBoolean,表示是否使用批次)和qtyFloat,表示使用的数量每个批次)。

另一方面,在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


    【解决方案1】:

    在计算域中使用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
    )
    

    【讨论】:

    • 我试过了,以防万一,但结果与store=True 相同。我想将store=True设置为TransientModel的字段没有多大意义,因为不会存储那种模型的记录。
    • 然后试试class class_name(models.Model)
    • 它必须是TransientModel,因为它是一种特定的形式,用于选择一些值并应用一些操作,而不是用于存储新记录。
    • 我认为,要访问一个值,您必须存储它。否则(最坏的情况),将所有字段放在单一表单视图中,然后您可以在 on_change 方法中访问 total_qty_selected,我不知道它是否适合您的要求。
    • @forvas store=True 在 TransientModel 上有意义。 Odoo 确实为这个模型在数据库中创建了一个表。它只是短暂的,因为该表的记录在过期时会被删除。见_transient_vacuumodoo/models.py
    【解决方案2】:

    最后,我设法用一个可怕的解决方法解决了这个问题。但是效果很好。

    我在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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 2021-08-02
      • 2023-01-03
      相关资源
      最近更新 更多