Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
3 Odgovori
6554 Prikazi
@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
Avatar
Opusti
Best Answer

Hi,

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


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


Thanks

Avatar
Opusti
Best Answer

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']

Avatar
Opusti
Best Answer

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.



Avatar
Opusti
Related Posts Odgovori Prikazi Aktivnost
2
jun. 24
2094
1
dec. 23
1859
1
apr. 23
1864
1
mar. 23
2819
0
dec. 22
2361