Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
2489 Visualizzazioni

Hi all, please help me to write onchange function in timesheet app.

I have 2 fields. 

one is base field that is Hours spent(unit_amount)

another one custom field that i created is bud(x_studio_bud)

This custom field value should not exceed base field value. If it exceed i need to show an error.

Example: 

base field value is 10.00

if i type greater value above than base field value. for example 10.10, it should show an error message.


Please help me to do this. I am using ODOO 15 enterprise edition.

Many thanks


Avatar
Abbandona
Risposta migliore

Hi Abdul Razak,

You can use the below code with your field names to show an error message.

from odoo import _
from odoo.exceptions import UserError


@api.onchange('base_field', 'custom_field')
def onchange_field(self):
if self.base_field and self.custom_field > self.base_field:
raise UserError(_("CUSTOM FIELD value should be less than BASE FIELD value."))

Regards

Avatar
Abbandona
Autore

Hi many thanks for the reply.

I am using enterprise edition 15. i have pasted this code in the compute field. I have pasted the exact code which is above and I have replace the field name. here is my code and editing.

from odoo import _
from odoo.exceptions import UserError

@api.onchange('unit_amount', 'x_studio_budget_hours')
def onchange_field(self):
if self.unit_amount and self.x_studio_budget_hours > self.unit_amount:
raise UserError(_("Value exceeded"))

but iam facing error.
ERROR:
Traceback (most recent call last):
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 643, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/odoo/src/odoo/15.0/odoo/http.py", line 301, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: forbidden opcode(s) in 'from odoo import _\nfrom odoo.exceptions import UserError\n\n\n@api.onchange(\'unit_amount\', \'x_studio_budget_hours\')\ndef onchange_field(self):\n if self.unit_amount and self.x_studio_budget_hours > self.unit_amount:\n raise UserError(_("Value exceeded"))': IMPORT_NAME, IMPORT_FROM

Please help....

Post correlati Risposte Visualizzazioni Attività
1
ott 24
2173
1
giu 23
2136
1
gen 22
2778
5
giu 17
5145
1
mag 23
2391