Skip to Content
Menu
This question has been flagged
1 Reply
3405 Views

Hi,

I have this class :

class Custom_Project (models.Model): 

_inherit = "project.task"

task_lines = fields.One2many('task.line','task_id',string="Lines")

 state = fields.Selection((('draft',_('Draft')),('open',_('In progress')),('done',_('Done'))), default = 'draft', string='Status', required="True")

What i want is to change the state of the workflow to "done" when  the value of the field "progress" in all lines of  the one2many tree "task_lines" is equal to 100.

So in my custom module (in the .py file) I tried

 @api.onchange( 'task_lines.progress')

 def progress_on_change(self):

      for record in self:

            all_done = True

            for line in record.task_lines:

                   if line.progress != 100:

                       all_done = False

                       break

            if all_done == True:

                   record.write({'state': 'done'})

But this does not change the state to done as i expected.  What should I do to make it work?

Thanks,

Avatar
Discard
Best Answer

I hope you want that 

task_lines = fields.One2many('task.line', 'task_id', string="Lines")
state = fields.Selection([ ('draft', 'Draft'),
                             ('open', 'In progress'),
                             ('done', 'Done'),],
                            default='draft', string='Status', required="True")
@api.constrains('state')def _progress_on_change(self):
    all_done = True
    for line in self:
        task_line = self.env['task.line'].search([('id', '=', line)],)
        for line in task_line:
        if line.progress != 100:
            all_done = False
            break

        if all_done == True:
            line.write({'state': 'done'})
Avatar
Discard