Skip to Content
Menu
This question has been flagged
2 Replies
4490 Views

Hi 

I have those two classes

First

class my_participation(models.Model):

_name = "participation.participation"

_name = "participation.participation" 

_description = "participation"
_rec_name="participation_num"

solde = fields.Integer(string="Solde")
fidelite = fields.Many2one("fidelite.fidelite", "Fidelite ", ondelete="cascade", required="true")


Seconde

class my_fidelite(models.Model):

_name = "fidelite.fidelite"
_description = "fidelite"
_rec_name="fidelite_num"

fidelite_num = fields.Char(string='Code de fidelite', required="true")
_sql_constraints = [ ('fidelite_c_unique', 'unique (fidelite_num)', 'Ce code EXIST ')]
participation_ids = fields.One2many("participation.participation", "fidelite", "Participations concernees")

sum_participations_solde = fields.Integer(string='Solde of fidelite')

my question is how can i calculate  sum_participations_solde that presents the sum of all soldes of participations that correspond to this fidelite ?

THANK YOU

Avatar
Discard
Author Best Answer

I worked with this code


sum_participation_soldes = fields.Integer(string="Solde totale de fildelite de ce mois ", compute='fidelite_total_solde', store=True)
#@api.depends('participation_ids')
def fidelite_total_solde(self):
total = 0
for r in self.participation_ids:
     total += r.solde
self.sum_participation_soldes = total


there is no error but it doesnt fuction the sum_participation_soldes not counted always 0 its not adding 

thank you

Avatar
Discard

self.sum_participation_soldes = sum(line.solde for line in self.participation_ids)

Author

If you mean like this it doesnt function too but no error

@api.depends('participation_ids')

def fidelite_total_solde(self):

for r in self.participation_ids:

self.sum_participation_soldes = sum(line.solde for line in self.participation_ids)

@api.depends('participation_ids')

def fidelite_total_solde(self):

self.sum_participation_soldes = sum(line.solde for line in self.participation_ids)

Author

Hi bouth of those doesnt function always 0

@api.depends('participation_ids')

def fidelite_total_solde(self):

for r in self:

r.sum_participation_soldes = sum(line.solde for line in r.participation_ids)

#@api.depends('participation_ids')

#def fidelite_total_solde(self):

# self.sum_participation_soldes = sum(line.solde for line in self.participation_ids)

Best Answer

Hi,

You can make it as a compute field and compute the sum into the field.

sum_participations_solde = fields.Integer(string='Solde of fidelite', compute='get_total_sum')

Now you have to write the function in the given name.

def get_total_sum(self):

    total = 0

    for rec in self.participation_ids:

          total += rec.solde

    self.sum_participations_solde = total

 The code is not tested, you can follow the same procedure, if the above is not working see how the amount of the sale order is calculated. There is a function to compute the total amount.


Thanks

Avatar
Discard