Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
3 Відповіді
6568 Переглядів
@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.



Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
2
черв. 24
2120
1
груд. 23
1867
1
квіт. 23
1877
1
бер. 23
2828
0
груд. 22
2380