Welcome!

Share and discuss the best content and new marketing ideas, build your professional profile and become a better marketer together.

Sign up

You need to be registered to interact with the community.
This question has been flagged
1 Reply
9 Views

Hey Cyllo community! Anyone got tips on setting up a compute field? I'm struggling a bit.

Avatar
Discard

Here's how you can achieve computed fields that store values in the database, focusing on the efficient use of 'store=True' and '@api.depends'. Computed fields automatically calculate values based on other fields or complex logic, providing dynamic data that updates when dependencies change. 'store=True' is used when we want a field to be saved in the database and not recalculated every time a form renders. Only compute fields with dependencies can be stored; otherwise, they wouldn't calculate properly. '@api.depends' is used to specify which fields the computed field depends on. class SaleOrder(models.Model): _inherit = 'sale.order' total_quantity = fields.Float( compute='_compute_total_quantity', store=True ) @api.depends('order_line.product_uom_qty') def _compute_total_quantity(self): for record in self: record.total_quantity = sum(record.order_line.mapped('product_uom_qty'))

Avatar
Discard

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!