Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
3 Trả lời
6559 Lượt xem
@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
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

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


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


Thanks

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.



Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 6 24
2099
1
thg 12 23
1863
1
thg 4 23
1867
1
thg 3 23
2822
0
thg 12 22
2366