pythonodooerp

Get a sale.order with an ID in other model [Odoo]


In the model account_move, I have a field called "invoice_origin" that have the ID of a sale order, but I need the entire sale.order. Both models are not related. How can I search and return the entire sale.order, in the move_account file?

I've tried something like this but it doesn't work

client_order_ref = fields.Char('get_sale_order().client_order_ref')

# Get a sale order with the invoice_origin field
def get_sale_order(self):
    sale_order = self.env['sale.order'].search([
        ('name', '=', self.invoice_origin)
    ])
   
    return sale_order

ERROR psycopg2.errors.UndefinedColumn: column account_move.client_order_ref does not exist LINE 1: ...ier_move_reference" as "supplier_move_reference", "account_m...


Solution

  • You passed 'get_sale_order().client_order_ref' as first parameter to Char, Odoo will use it as a label for client_order_ref field and the get_sale_order function will not be called.

    To compute the value of a field, use the compute parameter

    Example:

    client_order_ref = fields.Char(compute='get_sale_order')
    
    @api.depends('invoice_origin')
    def get_sale_order(self):
        for move in self:
            sale_order = self.env['sale.order'].search([
                ('name', '=', move.invoice_origin)
            ])
    
            move.client_order_ref = sale_order.client_order_ref
    

    Note that move lines are related to sale lines with sale_line_ids field