Search code examples
pythonodoo

Odoo - add custom field related to Sale Order


as below I added sub.name field to Sale Order model and it working well.

class sales_order_imcc(models.Model):
    _inherit = 'sale.order'
    sub_name = fields.Char(string="Subject Name")

now I need to add this field sub_name to Stock picking model but I got an internal server error

class stock_exp(models.Model):
    _inherit = 'stock.picking'
    sub_name = fields.Char(string="SO Reference", related="sale_order_id.sub_name")

Solution

  • sale_order_id should be in stock.picking model if not you will get an error. Before Odoo 10, a computed field named sale_id was added in sale_stock.

    Example of Odoo 8:

    def _get_sale_id(self, cr, uid, ids, name, args, context=None):
        sale_obj = self.pool.get("sale.order")
        res = {}
        for picking in self.browse(cr, uid, ids, context=context):
            res[picking.id] = False
            if picking.group_id:
                sale_ids = sale_obj.search(cr, uid, [('procurement_group_id', '=', picking.group_id.id)], context=context)
                if sale_ids:
                    res[picking.id] = sale_ids[0]
        return res
    
    _columns = {
        'sale_id': fields.function(_get_sale_id, type="many2one", relation="sale.order", string="Sale Order"),
    }  
    

    The sale_id is converted to a related field starting from version 10, you can see that in the following example of Odoo 13:

    
    class StockPicking(models.Model):
        _inherit = 'stock.picking'
    
        sale_id = fields.Many2one(related="group_id.sale_id", string="Sales Order", store=True, readonly=False)
    
    

    You can use the same logic depending on you Odoo version to get the value of sub_name from sale.order.