Search code examples
odoofieldodoo-view

related attributes , related fields on inherited view odoo


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.

here is an example of what I'm doing


Solution

  • 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.