Search code examples
pythonodoo-13

Read one2many field in inherited model


In odoo v13, the crm.lead model is inherited and modified by the sale_crm module.

In the sale_crm module, the model crm.lead is inherited and a one2many field is added, order_ids. This is an array of sales orders associated with the lead.

I am trying to inherit the crm.lead model, and create a new field that is computed using the order_ids field.

  1. I added sale_crm in the manifest dependencies

  2. I inherit the crm.lead model and attempt to concat the names of all the associated SOs:

    class Co7Lead(models.Model):
     _inherit = "crm.lead"
    
     so_list = fields.Text(
         compute='_get_sos_text',
         string="Text list of associated SOs",
         help="A comma separated list of SOs associated with this lead")
    
     def _get_sos_text(self):
         txt = ""
    
         for order in super(Co7Lead, self).order_ids:
             txt += order.name + ""
    
         return txt
    

Unfortunately, this causes a stack overflow (haha!)

I believe I need to use .browse on the order_ids field but I'm not able to find any examples on how to do this.


Solution

  • The compute method must assign the computed value to the field. If it uses the values of other fields (order_ids.name), it should specify those fields using depends().

    You don't need to use super here, self is a record set, so loop over it to access the value of order_ids for each record.

    Example:

    @api.depends('order_ids.name')
    def _get_sos_text(self):
        for lead in self:
             lead.so_list = "\n".join(order.name for order in lead.order_ids)