what is related attribute , what it can be used for ? and how to add a related attribute . I'm trying to add a related field for total amount.
3 Answers
In context of Odoo related field means, that its value will be
- read from another table (related table) -->
store=False
- read from another table, but stored in the table its defined on -->
store=True
Examples (Odoo V8):
sale.order
currency_id: it is persisted in product_priceliststock_quant
packaging_type_id: it is persisted in product_packaging and stock_quant. Everytime you change the value on product_packinging it will be updated on stock_quant, too, not vice versa.

- 13,553
- 3
- 25
- 38
**Related Field* is used when you wanted to pull a value from another model you can use related field on fields.
Here is an example for you.
order_id = fields.Many2one(comodel_name='sale.order', 'Sale Order')
order_amount = fields.Monetary(string='Order Amount',
store=True,
related='order_id.amount_total')
you must add a Many2one field in the model which relates to the model you want to access a field. in our case model is sale.order.
with the related kwarg you can relate field of the related model defined in the Many2one field. in our case order_id.
Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated.(Reference)
Hope this helps!

- 16,054
- 6
- 50
- 58
Related Field
In such case we need to take the value from any other table but we already have the reference of that table in our model, in that case we can utilize one functionality with that we can add the multiple fields from the reference table by just having the only one reference field in our model.
One relational field (Many2one) of the source model is mandatory to have in the destination model to create relational field.
Consider the company_currency_id field from the res.currency model is there in the destination model then you can get the current rate of that currency in your target model.
syntax: 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),
}
Here,
company_currency_id => the field in the same model through which the new field will be relate,
rate_silent => is the field which you are going to relate with new field, means the field from source model,
relation => is the source model name,
type => is the datatype of the source field
Syntax: v8 and newer version
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.

- 14,598
- 5
- 45
- 75