2016-09-21 47 views
0

环境为什么计算字段不能从Odoo8中的TransientModel访问?

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

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

另一方面,在lots.manager模式,我有一个名为total_qty_selected一个计算字段,计算所有可用批次,其selectedqty的总和。

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) 

问题

计算字段total_qty_selected计算好(我表现出来的意见和伟大工程),但是,当我尝试从product.lot.available阅读我总是得到0.例如,上述onchange函数中的_logger行显示产品右侧的名称,但total_qty_selected返回0,而不是在那一刻I c在表格中读取2.00,或者不同于0的任何值。

我需要在onchange函数中获得正确的值。

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

回答

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

我试过了,为了以防万一,但相同的结果与'商店= TRUE'。我想'设置'store = True'是一个'TransientModel'的字段没什么意义,因为这种模型的记录不会被存储。 – forvas

+0

然后尝试class class_name(models.Model) – KbiR

+0

它必须是'TransientModel',因为它是一种特定的形式,用于选择某些值并应用某些操作,但不用于存储新记录。 – forvas

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) 
相关问题