環境爲什麼計算字段不能從Odoo8中的TransientModel訪問?
我已經創建了兩個TransientModel
(命名爲lots.manager
和product.lot.available
),用(很多經理都會有幾種可用批次),它們之間的關係One2many
。
我的目的是顯示可用批次的列表,用戶將能夠選擇他想要使用的批次以及每個批次的數量。所以product.lot.available
有lot_id
(選擇很多),selected
(一個Boolean
,它表示是否使用的批次)和qty
(一個Float
,它表明每批次使用的數量)字段。
另一方面,在lots.manager
模式,我有一個名爲total_qty_selected
一個計算字段,計算所有可用批次,其selected
場真的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)
問題
計算字段total_qty_selected
計算好(我表現出來的意見和偉大工程),但是,當我嘗試從product.lot.available
閱讀我總是得到0.例如,上述onchange
函數中的_logger
行顯示產品右側的名稱,但total_qty_selected
返回0,而不是在那一刻I c在表格中讀取2.00,或者不同於0的任何值。
我需要在onchange函數中獲得正確的值。
任何人都可以幫助我如何管理?
我試過了,爲了以防萬一,但相同的結果與'商店= TRUE'。我想'設置'store = True'是一個'TransientModel'的字段沒什麼意義,因爲這種模型的記錄不會被存儲。 – forvas
然後嘗試class class_name(models.Model) – KbiR
它必須是'TransientModel',因爲它是一種特定的形式,用於選擇某些值並應用某些操作,但不用於存儲新記錄。 – forvas