odooodoo-13odoo-14odoo-15

Odoo 15 Find tax_ids Inside the account.move.line (Invoice) Model


Good day, hope everything's well.

I'm trying to find the value of tax_ids (Many2many field) inside the account.move.line model but i can't seems to find anything. I already access the psql of the database but i cant find tax_ids too.

I accessed that account.move.line model with ORM like this :

    def _post(self, soft=True):
        for move in self:
            ....
            account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
            print(account_move_line.tax_ids) #this find nothing

could someone please elaborate how is it possible to access id of the tax that applied to, in this case, an invoice line record?

Edit : Sometimes this ORM fetching the ID and sometimes it doesn't. But most likely it's not fetching.


Solution

  • I'm trying to find the value of tax_ids (Many2many field) inside the account.move.line model but i can't seems to find anything. I already access the psql of the database but i cant find tax_ids too.

    tax_ids in account.move.line model is a Many2Many field, which is stored separately as another table in the database. This kind of relation field mostly be named something like this (main_table)_(related_table)_rel (ignore the parentheses). For example, this particular case's table is account_move_line_account_tax_rel since its main table is account_move_line and the related table for that specific field is account_tax. Inside the relation table, you will almost always find 2 fields mapping both ids together. For this case, it is going to be account_move_line_id and account_tax_id.

    I accessed that account.move.line model with ORM like this :

    def _post(self, soft=True):
        for move in self:
            ....
            account_move_line = self.env['account.move.line'].search([('id', '=', int(move.id))])
            print(account_move_line.tax_ids) #this find nothing could someone please elaborate how is it possible to access id of the tax
    

    that applied to, in this case, an invoice line record?

    Edit : Sometimes this ORM fetching the ID and sometimes it doesn't. But most likely it's not fetching.

    Accessing a field via ORM always works as you intended if it is implemented correctly. In your code, you are searching account_move_line by id but you are trying to search it with move.id, which I assume it is account_move since you got the data sometimes. If you can access the lines that you want correctly, you will see that account_move_line.tax_ids will always give you the correct tax_ids. From what your code looks, I think you are trying to search the lines by its account_move. Therefore, your domain should be [('move_id', '=', int(move.id))] instead.