This question has been flagged
1 Reply
9373 Views

Hi,

I need to pull through a boolean field from res.partner to account.invoice in order to filter invoices based on Partner data.

From what I've read this can be accomplished through a related field. I can see there's some documentation here -

https://www.odoo.com/documentation/8.0/reference/orm.html and a different example here -

https://odedrabhavesh.blogspot.co.uk/2015/02/how-related-field-work-in-odoo.html however I'm not to sure where this code would go. The second link seems to be suggesting that I need to create a module from the looks of it? It's also not very clear what each parameter does in the second example:


class sale_order_line(osv.Model):
    _inherit = 'sale.order.line'

    _columns = {
        'product_categ_name': fields.related('product_id', 'categ_id', 'name', type='char', string='Product Category', store=True, readonly=True ),
    }

_inherit - I assume this is the model that we want to modify

product_categ_name - name of the related field

product_id - id of the record we are linking to, in this case Product model

categ_id - ?

name - I assume this is name of the field from the other model that we want the value to be?

type - field type

string - field label

store - whether to store the computed value on the database

readonly - read only or not


In my case I would need to do something like this?

class bank_account_active(osv.Model):
    _inherit = 'account.invoice'

    _columns = {
        'bank_account_active': fields.related('partner_id', '???', 'restrict_invoice', type='boolean', string='Bank Account Active', store=True, readonly=True ),
     }


If anyone can push me into the right direction would great. I haven't done much python with Odoo just yet, outside from adding some python expressions through the interface, which is probably why I'm feeling a little lost here.

Thanks.

Avatar
Discard
Best Answer

Hi,

Sometimes you need to refer to the relation of a relation. For example, supposing you have objects: City -> State -> Country, and you need to refer to the Country from a City, you can define a field as below in the City object:

Code for old API (Version7)

'country_id': fields.related(
    'state_id',
    'country_id',
    type="many2one",
    relation="res.country",
    string="Country",
    store=False)

Here " 'state_id','country_id' " shows complete sequence of fields to traverse to reach the desired field.


Code for old API

    country_id: fields.Many2one(related='state_id.country_id', string="Country")

Here related attribute shows complete sequence of fields to traverse to reach the desired field.

Instead of Many2one field we can make this char with country name by following changes:

'country_id': fields.related(
    'state_id',
    'country_id',   
 'name', type="char", string="Country", store=False)
    country_id: fields.Char(related='state_id.country_id.name', string="Country")


Hope this helps.


Avatar
Discard
Author

Great! Although it's pretty obvious now, I didn't realise it was linking to the country_id/name fields in that way. I'm now able link through to the field that I wanted from the invoice record. Thanks for the help.