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
CodePudding user response:
I can't test it, and you did not show what error you got.
You should always provide values in the compute functions. And use false if you don't have anything to put there.
Try to use field commands. https://www.odoo.com/documentation/master/developer/reference/backend/orm.html#odoo.fields.Command
@api.depends("odoo_product_id")
def _compute_offers(self):
for rec in self:
rec.shop_offer_ids = False
if rec.odoo_product_id:
offers = self.env['demo.offers'].search
([('product_id','=',rec.odoo_product_id.id)])
if offers:
rec.shop_offer_ids = [(6, 0, offers.ids)]
CodePudding user response:
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