2016-09-21 43 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) 
相關問題