如何处理 Odoo 8 瞬态模型中的相关字段和计算字段?
How to deal with related and computed fields in Transient Models in Odoo 8?
我有一个我无法理解的问题。我向模型添加了两个新字段 stock.production.lot
。这些字段是计算的浮点数,并且工作完美。他们的名字是 current_qty
和 expected_qty
.
然后,我创建了一个瞬态模型。在这一个中,我创建了一个指向模型 stock.production.lot
的 many2one 字段 lot_id
。我添加了两个浮点字段,也分别命名为 current_qty
和 expected_qty
,它们与我上面提到的相应字段相关。
问题是只有 current_qty
得到了正确的值(另一个总是得到零)。
示例:
我创造了很多。当我这样做时,current_qty
被计算并且值为 10.000,expected_qty
也被计算并且值为 5.000.
现在我打开瞬态模型,current_qty
得到 10.000,这是正确的,但是 expected_qty
得到 False。为什么?
我什至在瞬态模型中添加了其他字段。这是一个计算字符,应该显示下一个:current_qty (expected_qty),按照本例的值,它应该是 10.000 (5.000)。但它也采用 False 值(它也不写 current_qty
)。此外,我在计算方法中写入日志消息来检查该方法是否被调用以及它采用了哪些值。令人惊讶的是,日志中显示的 current_qty
和 expected_qty
的值是正确的! (10.000 和 5.000)。
Python代码
class ProductLotAvailable(models.TransientModel):
_name = 'product.lot.available'
@api.multi
@api.depends('current_qty', 'expected_qty')
def _compute_beautiful_qty(self):
for available_lot in self:
current_qty = str(available_lot.current_qty)
_logger.info(current_qty)
expected_qty = str(available_lot.expected_qty)
_logger.info(expected_qty)
available_lot.beautiful_qty = current_qty + '(' + expected_qty + ')'
lot_id = fields.Many2one(
comodel_name='stock.production.lot',
string='Lot',
readonly=True,
)
current_qty = fields.Float(
related='lot_id.current_qty',
string='Current lot quantity',
readonly=True,
digits=dp.get_precision('Product Unit of Measure'),
)
expected_qty = fields.Float(
related='lot_id.expected_qty',
string='Expected lot quantity',
readonly=True,
digits=dp.get_precision('Product Unit of Measure'),
)
beautiful_qty = fields.Char(
compute='_compute_beautiful_qty',
string='Current lot quantity',
readonly=True,
)
XML代码
<field name="product_lots_available" nolabel="1">
<tree create="false" delete="false" editable="bottom">
<field name="current_qty" invisible="0"/>
<field name="expected_qty" invisible="0"/>
<field name="beautiful_qty"/>
<field name="lot_id"/>
</tree>
</field>
谁能帮我解决这个问题?我的代码中可能有一个简单的错误,我在过去几个小时内没有看到它。
我这么想的唯一原因是因为它是相关字段,所以只有在您保存该记录后才会放置该字段中的值。由于此模型是瞬态模型,因此向导会打开,但您无法再次打开同一条记录来验证该结果。
要验证它,如果转到数据库并检查记录,您将在那里获得值。双重确保只需保持这两个字段的功能 Store=False,因此值将始终存在。
我没有在您的代码中发现任何其他问题。
我发现了问题:我安装了另一个模块,它覆盖了为瞬态模型加载默认值的方法。此方法是填写current_qty
,而不是expected_qty
。 beautiful_qty
.
同样的问题
所以尽管我引入了我需要的值,但它们被另一个模块覆盖了。
我有一个我无法理解的问题。我向模型添加了两个新字段 stock.production.lot
。这些字段是计算的浮点数,并且工作完美。他们的名字是 current_qty
和 expected_qty
.
然后,我创建了一个瞬态模型。在这一个中,我创建了一个指向模型 stock.production.lot
的 many2one 字段 lot_id
。我添加了两个浮点字段,也分别命名为 current_qty
和 expected_qty
,它们与我上面提到的相应字段相关。
问题是只有 current_qty
得到了正确的值(另一个总是得到零)。
示例:
我创造了很多。当我这样做时,current_qty
被计算并且值为 10.000,expected_qty
也被计算并且值为 5.000.
现在我打开瞬态模型,current_qty
得到 10.000,这是正确的,但是 expected_qty
得到 False。为什么?
我什至在瞬态模型中添加了其他字段。这是一个计算字符,应该显示下一个:current_qty (expected_qty),按照本例的值,它应该是 10.000 (5.000)。但它也采用 False 值(它也不写 current_qty
)。此外,我在计算方法中写入日志消息来检查该方法是否被调用以及它采用了哪些值。令人惊讶的是,日志中显示的 current_qty
和 expected_qty
的值是正确的! (10.000 和 5.000)。
Python代码
class ProductLotAvailable(models.TransientModel):
_name = 'product.lot.available'
@api.multi
@api.depends('current_qty', 'expected_qty')
def _compute_beautiful_qty(self):
for available_lot in self:
current_qty = str(available_lot.current_qty)
_logger.info(current_qty)
expected_qty = str(available_lot.expected_qty)
_logger.info(expected_qty)
available_lot.beautiful_qty = current_qty + '(' + expected_qty + ')'
lot_id = fields.Many2one(
comodel_name='stock.production.lot',
string='Lot',
readonly=True,
)
current_qty = fields.Float(
related='lot_id.current_qty',
string='Current lot quantity',
readonly=True,
digits=dp.get_precision('Product Unit of Measure'),
)
expected_qty = fields.Float(
related='lot_id.expected_qty',
string='Expected lot quantity',
readonly=True,
digits=dp.get_precision('Product Unit of Measure'),
)
beautiful_qty = fields.Char(
compute='_compute_beautiful_qty',
string='Current lot quantity',
readonly=True,
)
XML代码
<field name="product_lots_available" nolabel="1">
<tree create="false" delete="false" editable="bottom">
<field name="current_qty" invisible="0"/>
<field name="expected_qty" invisible="0"/>
<field name="beautiful_qty"/>
<field name="lot_id"/>
</tree>
</field>
谁能帮我解决这个问题?我的代码中可能有一个简单的错误,我在过去几个小时内没有看到它。
我这么想的唯一原因是因为它是相关字段,所以只有在您保存该记录后才会放置该字段中的值。由于此模型是瞬态模型,因此向导会打开,但您无法再次打开同一条记录来验证该结果。
要验证它,如果转到数据库并检查记录,您将在那里获得值。双重确保只需保持这两个字段的功能 Store=False,因此值将始终存在。
我没有在您的代码中发现任何其他问题。
我发现了问题:我安装了另一个模块,它覆盖了为瞬态模型加载默认值的方法。此方法是填写current_qty
,而不是expected_qty
。 beautiful_qty
.
所以尽管我引入了我需要的值,但它们被另一个模块覆盖了。