相关属性,继承视图odoo上的相关字段

related attributes , related fields on inherited view odoo

什么是相关属性,它有什么用?以及如何添加相关属性。 我正在尝试为总金额添加一个相关字段。

在 Odoo 相关字段的上下文中,它的值将是

  1. 从另一个 table 读取(相关 table)--> store=False
  2. 从另一个 table 读取,但存储在其定义的 table --> store=True

示例(Odoo V8):

  1. sale.order currency_id: 在product_pricelist
  2. 中持久化
  3. stock_quant packaging_type_id:在product_packaging和stock_quant中持久化。每次您更改 product_packinging 上的值时,它也会在 stock_quant 上更新,反之亦然。

**相关字段* 当您想从另一个模型中提取值时使用,您可以在字段上使用相关字段。

这是一个例子。

order_id = fields.Many2one(comodel_name='sale.order', 'Sale Order')
order_amount = fields.Monetary(string='Order Amount',
                               store=True,
                               related='order_id.amount_total')

您必须在模型中添加一个Many2one字段,该字段与您要访问字段的模型相关。在我们的例子中模型是 sale.order.

使用 related kwarg,您可以关联 Many2one 字段中定义的相关模型的字段。在我们的案例中 order_id

设置 store kwarg 将自动将值存储在数据库中。使用新的API,相关字段的值将自动更新。(Reference)

希望对您有所帮助!

相关领域

在这种情况下,我们需要从任何其他 table 中获取值,但我们已经在我们的模型中引用了那个 table,在这种情况下,我们可以利用我们的一个功能我们的模型中只有一个参考字段,可以从参考 table 添加多个字段。

One relational field (Many2one) of the source model is mandatory to have in the destination model to create relational field.

考虑来自 res.currency 模型的 company_currency_id 字段在目标模型中,然后您可以获取目标模型中该货币的当前汇率。

语法:v7

_columns = {
'current_rate': fields.related('company_currency_id','rate_silent', type='float', relation='res.currency',digits_compute=dp.get_precision( 'Account'), string='Current Rate', readonly=True),
}

这里,

company_currency_id => 新字段关联的同一模型中的字段,

rate_silent => 是您要与新字段关联的字段,表示来自源模型的字段,

relation => 是源模型名称,

type => 是源字段的数据类型

语法:v8 及更新版本

current_rate = fields.Float(string='Current Rate', related='company_currency_id.rate_silent', store=True, readonly=True)

Note :

Value is available in that field only after you save the record.

When you update value in your newly defined related field it will be updated in source field as well, though it's always advisable to set readonly in newly defined field.