Skip to Content
Menu
This question has been flagged
1 Reply
2077 Views

Hi, I'm trying to create a particular function.

Let's say I'm analizying a gym, and I create for every member a number of annual plans (let's ignore how many and their timing - the important thing is that it could be more than one for each member). Every annual plan is made of a number of enrollment to some courses, and every enrollment to a course has a specific price. So, what I'm trying to do is to make the function that computes the total price of a generic annual plan.

I made a model for the courses, with the following fields in particular

_name = 'piano.corso'
costo_a_persona = fields.Monetary(string="Costo (a persona)", required=True)
currency_id = fields.Many2one("res.currency", string="Valuta", required=True)
corso_piano = fields.Many2one('piano.piano',string="Piano di formazione di cui fa parte", required=True) 

And I made a model for the annual plans, with the following fields in particular

_name = 'piano.piano'
corsi_piano = fields.One2many('piano.corso','corso_piano',string='Edizioni erogate nel piano di formazione') 

The problem is that I should write the function in the annual plans file, but the prices of the courses aren't saved there - so, how can I make such function? I tried this, but it gave me back the following error:

currency_id = fields.Many2one("res.currency", string="Valuta",required=True)    
costo_totale_piano = fields.Monetary(compute="_get_costo_totale_piano", currency_field="currency_id", string="Costo totale del piano", readonly=True)

@api.depends("corsi_piano","corsi_piano.costo_a_persona")
def _get_costo_totale_piano(self):
total = 0
for r in self:
for i in r.edizioni_piano:
total = total + r.edizioni_piano.costo_totale[i]
r.costo_totale_piano = total 
->ValueError: Expected singleton: piano.corso(1, 3, 6)
Avatar
Discard
Author

@sonia thank you!

Best Answer

def _get_costo_totale_piano(self):        
total = float(0)
for r in self:
for rec in r.edizioni_piano:
total = total + float(rec.costo_totale)
r.costo_totale_piano = total 

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 22
4785
1
Nov 24
1483
1
Nov 24
1188
2
Sep 24
1047
1
Aug 24
2451