콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
3 답글
6595 화면
@api.multi
@api.depends
('totalfee')
def _get_details_am(self):
cr = self.env.cr
for record in self:
# if record.appl_id:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
for each in returned_record:
if each['am']:
print each['am']
if record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']
elif record.total_amount < each['am']:
#pass
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')

in the above method
when i click save button ValidationError is displaying and recording is storing in database


please help me.....
thanks in advance
아바타
취소
베스트 답변

Hi,

Try to use @api.constrains decorator, it will work when saving the record.


@api.constrains('totalfee')
def _get_details_am(self):


Thanks

아바타
취소
베스트 답변

Hi, 

If I understand you correctly, You need to validate all records first and show an error if the amount less than total amount without saving anything to the DB. If this is the case then you have to validate all records first and raise an error if your condition is met and if all records data is okay then you will save it to the DB. 

If self have only one record you can use the below code:


@api.multi
@api.depends('totalfee')
def _get_details_am(self):
cr = self.env.cr
for record in self:
# if record.appl_id:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
if any(record.total_amount < each['am'] for each in returned_record):
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')
else:
for each in returned_record:
if each['am']:
if record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']


If self has multi record and you want to validate them all before saving them to the DB, Use the below:

@api.multi
@api.depends('totalfee')
def _get_details_am(self):
cr = self.env.cr
# This loop to check all records first and raise an error if
for record in self:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
if any(record.total_amount < each['am'] for each in returned_record):
raise ValidationError('For Full Payment, Use the `Full Fee Payment` Option')


for record in self:
cr.execute(
"SELECT sum(amount) as am FROM ibprogram_studentchild where child_id IN (select id from ibprogram_studentparent where id=%s)",
(record.id,))
returned_record = cr.dictfetchall()
if returned_record:
for each in returned_record:
if each['am'] and record.total_amount >= each['am']:
record.totalfee = record.total_amount - each['am']

아바타
취소
베스트 답변

Add

from odoo.exceptions import ValidationError 

before defining any function took me a while to solve the similar kind of error.

Hope this might help you.



아바타
취소
관련 게시물 답글 화면 활동
2
6월 24
2130
1
12월 23
1875
1
4월 23
1893
1
3월 23
2841
0
12월 22
2389