如何在ODOO 10中停止抛出指定验证错误?(以日期校验为例)
Got it, let's figure out how to turn off that specific validation error in Odoo 10. First off, you really shouldn't modify core Odoo files directly—doing that will cause headaches when you upgrade later. Instead, use Odoo's built-in inheritance system. Here are your go-to options:
Option 1: Inherit the Model and Override the Constraint (Recommended)
This is the best practice because it keeps your changes separate from core code, so upgrades won't wipe them out. You'll need to create a custom module (if you don't have one already) and add this code to its Python file:
from odoo import models, api class ProjectTask(models.Model): _inherit = 'project.task' @api.constrains('date_start', 'date_end') def _check_dates(self): # Completely disable the date validation by doing nothing here pass # If you want to keep partial validation (e.g., only for certain tasks), # you can add custom logic instead. For example: # if self.env.user.has_group('some.group'): # super(ProjectTask, self)._check_dates()
After adding this, install your custom module, and the validation error will no longer trigger.
Option 2: Temporarily Comment Out the Core Code (Not Recommended)
Only use this for testing purposes—since modifying core files will be overwritten when you upgrade Odoo. To do this:
- Locate the
project.pyfile in Odoo's project module. - Find the
_check_datesmethod you mentioned. - Either comment out the entire method or just the line that raises the error:
Comment the entire method:
# @api.constrains('date_start', 'date_end') # def _check_dates(self): # if any(self.filtered(lambda task: task.date_start and task.date_end and task.date_start > task.date_end)): # raise ValidationError(_('错误!任务开始日期必须早于结束日期。'))
Or just comment the error raise line:
@api.constrains('date_start', 'date_end') def _check_dates(self): if any(self.filtered(lambda task: task.date_start and task.date_end and task.date_start > task.date_end)): # raise ValidationError(_('错误!任务开始日期必须早于结束日期。')) pass
Important Notes:
- Always prioritize Option 1—custom modules are the Odoo-approved way to modify behavior.
- If you use Option 2, make sure to back up your changes before any Odoo upgrade, as core files will be replaced.
- For the custom module approach, ensure your module declares a dependency on the
projectmodule in its__manifest__.pyfile.
内容的提问来源于stack exchange,提问作者Sachin Burnawal

