콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
3108 화면

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)
아바타
취소
작성자

@sonia thank you!

베스트 답변

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 

아바타
취소
관련 게시물 답글 화면 활동
1
7월 22
5973
0
8월 25
83
1
8월 25
2022
0
8월 25
5
1
7월 25
2556