1

I'm using compute method to add records to the one2many field and it's working fine in form view but not in tree view. Can anyone help?

@api.depends("odoo_product_id")
def _compute_offers(self):
    for rec in self:
        if rec.odoo_product_id:
            offers = self.env['demo.offers'].search
                ([('product_id','=',rec.odoo_product_id.id)])
            for offer in offers:
                rec.shop_offer_ids = rec.shop_offer_ids + offer

It's throwing Compute method failed to assign Error in tree view only

Ravi Singh
  • 307
  • 2
  • 13

1 Answers1

1

The compute method will fail to assign the field value for records where the odoo_product_id field is not set.

To fix that error use else to provide a default value for shop_offer_ids field.

Example:

@api.depends("odoo_product_id")
def _compute_offers(self):
    for rec in self:
        if rec.odoo_product_id:
            offers = self.env['demo.offers'].search
                ([('product_id','=',rec.odoo_product_id.id)])
            rec.shop_offer_ids = offers.ids
        else:
            rec.shop_offer_ids = []

You can find an example in website_event_questions module

Kenly
  • 24,317
  • 7
  • 44
  • 60