Odoo 10技术问询:如何根据条件更改工作流状态?
How to Auto-Set Task Status to "Done" When All One2many Lines Are 100% Complete in Odoo
Got it, let's work through this problem. You've inherited project.task and added a One2many field task_lines, and want the task's state to automatically switch to "done" whenever every line in task_lines has a progress value of 100. Here are two practical approaches to implement this in your custom module's .py file:
Approach 1: Real-Time Status Sync with Computed Field
This method keeps the task status in lockstep with line progress, updating automatically whenever any line's progress value changes.
from odoo import models, fields, api from odoo.tools.translate import _ class Custom_Project(models.Model): _inherit = "project.task" task_lines = fields.One2many('task.line', 'task_id', string="Lines") state = fields.Selection( selection=(('draft', _('Draft')), ('open', _('In progress')), ('done', _('Done'))), default='draft', string='Status', required=True, compute='_compute_task_state', store=True, readonly=False, # Let users manually adjust status when needed ) @api.depends('task_lines.progress') def _compute_task_state(self): for task in self: # Check if all lines have reached 100% progress all_lines_completed = all(line.progress == 100 for line in task.task_lines) # Auto-set to done if all lines are completed (and there are lines present) if task.task_lines and all_lines_completed: task.state = 'done' # Optional: Revert to "In progress" if a line's progress drops below 100 elif task.state == 'done' and not all_lines_completed: task.state = 'open' # Leave other states as-is to support manual status changes
Breakdown:
- The
@api.depends('task_lines.progress')decorator triggers the calculation anytime a line's progress value is updated. store=Truesaves the computed state to the database for better performance.readonly=Falselets users manually adjust the status, but the system will override it to "done" once all lines are complete. The revert logic can be removed if you don't want statuses to roll back automatically.
Approach 2: Check Status on Save (Write/Create)
If real-time sync isn't necessary, you can only update the status when the task is saved. This is lighter on performance for large datasets.
from odoo import models, fields, api from odoo.tools.translate import _ class Custom_Project(models.Model): _inherit = "project.task" task_lines = fields.One2many('task.line', 'task_id', string="Lines") state = fields.Selection( selection=(('draft', _('Draft')), ('open', _('In progress')), ('done', _('Done'))), default='draft', string='Status', required=True, ) def write(self, vals): # Save any changes first result = super(Custom_Project, self).write(vals) # Check and update status after saving for task in self: all_lines_completed = all(line.progress == 100 for line in task.task_lines) if task.task_lines and all_lines_completed and task.state != 'done': super(Custom_Project, task).write({'state': 'done'}) # Optional: Revert status if lines are no longer fully complete elif not all_lines_completed and task.state == 'done': super(Custom_Project, task).write({'state': 'open'}) return result @api.model def create(self, vals): # Create the task first task = super(Custom_Project, self).create(vals) # Check if initial lines are all complete all_lines_completed = all(line.progress == 100 for line in task.task_lines) if task.task_lines and all_lines_completed: task.write({'state': 'done'}) return task
Breakdown:
- The
writemethod runs after saving any changes to the task or its lines, then checks if all lines are complete to update the status. - The
createmethod ensures that if you create a task with pre-completed lines, it gets marked as done immediately.
Key Notes:
- Ensure your
task.linemodel has aprogressfield (e.g.,fields.Integer(string='Progress', default=0)). - If you want an empty
task_lineslist to count as "complete" (auto-set to done), remove thetask.task_lines andcondition from the checks (sinceall()on an empty list returnsTrue). - If your Odoo setup enforces workflow transitions (e.g., you can't jump directly from "draft" to "done"), adjust the logic to follow required states or update your workflow rules to allow the transition.
内容的提问来源于stack exchange,提问作者Salma
相关产品推荐
相关产品推荐

