Skip to Content
Menu
This question has been flagged
2 Replies
2794 Views

Intended function: When a user checks a box in the "log" model it triggers an onchange function that sets a boolean in the "task" model to True


What actually happens: The function does trigger, and there are no exceptions raised. However, the boolean in the "task" model does not become True.

I have no idea why. Thanks in advance!

Code:



class log(models.Model):
_name = 'custom_timesheets.log'
_description = 'Timesheet Logs'


employee = fields.Many2one('res.users','Employee', default=lambda self: self.env.user,required=True)
name = fields.Char(string="Log",default=lambda self: fields.datetime.today().strftime('%m/%d/%Y'))
related_task = fields.Many2one('custom_timesheets.tasks',string="Task")
mark_complete = fields.Boolean(string='Mark Complete?')
related_timesheet = fields.Many2one('custom_timesheets.timesheet',string='Timesheet',readonly=True,ondelete="cascade")
hours = fields.Float()
note = fields.Text()
date = fields.Date(string="Log date",default=lambda self: fields.datetime.today())

@api.onchange('mark_complete')
def _isdone(self):
for record in self:
if record.related_task:
for task in record.related_task:
task.complete = True



class tasks(models.Model):
_name = 'custom_timesheets.tasks'
_description = 'Timesheet Tasks'

name= fields.Char(String="Name")
note = fields.Text()
employee = fields.Many2one('res.users','Assigned To', required=True)
related_logs = fields.One2many('custom_timesheets.log','related_task',string="Logs")
priority = fields.Selection(selection=[('High','High'),('Medium','Medium'),('Low','Low')],string="Priority")
complete = fields.Boolean(string="Complete?")

Avatar
Discard

@api.onchange('mark_complete')

def _isdone(self):

if self.related_task:

self.env[''custom_timesheets.tasks'].sudo().browse(self.related_task.id).write({'complete': True})# Use Super Admin to update values

#self.env[''custom_timesheets.tasks'].browse(self.related_task.id).write({'complete': True})# Use Current User to update values

Author

This was the correct solution thank you!

Best Answer

onchange doesn't update read-only fields. In that case, we have to override the write and do the tweak. But here you have a writable field and nothing wrong can found.

if not working on onchange, do the same on write function too.

Avatar
Discard