Skip to Content
Menu
This question has been flagged
2 Replies
1486 Zobrazenia


from odoo import api, fields, models, _


class SyndicPaiement(models.Model):
_name = "syndic.paiement"
total_amount = fields.Float(string='Total Amount', compute="_compute_montant_total")
received_amount = fields.Float(string='Received Amount')

I failed to write this algorithm: total_amount += received_amount

I want that each time I receive a received_amount it is added to the total_amount

THANK YOU

Avatar
Zrušiť
Autor

Thank you for your help but it still send me this message: 

AttributeError: 'syndic.paiement' object has no attribute '_compute_montant_total'

Thanks

see if the function _compute_montant_total is added syndic.paiement model and service has been restarted

Best Answer

you can use the @api.onchange decorator to trigger a method that updates the total_amount field.

from odoo import api, fields, models


class SyndicPaiement(models.Model):
_name = "syndic.paiement"

total_amount = fields.Float(string='Total Amount', compute="_compute_montant_total")
received_amount = fields.Float(string='Received Amount')

@api.depends('received_amount')
def _compute_montant_total(self):
for record in self:
record.total_amount = sum(record.received_amount for record in self)

@api.onchange('received_amount')
def _onchange_received_amount(self):
self._compute_montant_total()

In this example, we define the total_amount field as a computed field with the compute attribute set to _compute_montant_total. The _compute_montant_total method sums up the received_amount values for all records in the current set.

The @api.onchange('received_amount') decorator is used for the received_amount field. Whenever the received_amount field is changed, the _onchange_received_amount method is triggered, which in turn calls the _compute_montant_total method to update the total_amount field.

Avatar
Zrušiť
Best Answer

Hi,

Please try this way,

@api.depends('received_amount')
def _compute_montant_total(self):
​for record in self:
​record.total_amount += record.received_amount

Thanks

Avatar
Zrušiť