This question has been flagged

When changing the state of a project, I need to write a number to the related product. Project and product have a many2many relationship:

Project_model: product_field -> many2many <- Product_model: number_field

This should be possible by adding "Automated Actions" to project model. I tried with something like that:

for record in records:
    product = record[('x_product)]
    product[('number')] = record.calc_value

When triggering the state change, I get this error:

"Expected singleton: product.template()" while evaluating
Avatar
Discard
Best Answer

The record['field_name']=value syntax is only used for a single record. Since 0, 1 or more products may exist, use records.write({'field_name':value})

Avatar
Discard
Author

Thanks for the answer, that looks promising.

But it seems that I have a basic problem with the relation: Do I need to load the product with something like that before I can access it?

products = env['product.template'].search(['id', '=', record.x_product])

Or do I have direct access to the product as it is already related with a many2many relation? Then I could directly apply the action:

record['x_product'].write({'number': record.calc_value})

None of the actually works.

PS: Is there any easy method to print values (like python print) in the Automated Actions?