I have two fields in my module,(start_date and end_date). I want to validate the date range as end_date must be greater than start_date and display error message like "End Date Should be greater than Start Date" This is mu cord.
from openerp.osv import osv, fields
class op_batch(osv.Model):
_name = 'op.batch'
_columns = {
'name': fields.char(size=25, string='Name', required=True),
'code': fields.char(size=15, string='Code', required=True),
'start_date': fields.date(size=15, string='Start Date', required=True),
'end_date': fields.date(size=15, string='End Date', required=True, onchange="validate_date_range"),
'state': fields.selection(
[('planned', 'Planned'), ('running', 'Running'), ('cancel', 'Cancel'), ('finished', 'finished')],
string='State'),
'course_id': fields.many2one('op.course', string='Course', ondelete='restrict', required=True),
}
def validate_date_range(self, cr, uid, vals, context=None):
en_date = date.start_date.value
st_date = date.end_date.value
if en_date < st_date:
raise Warning(_("End Date Should be greater than Start Date"))
return True
_sql_constraints = [('code', 'UNIQUE (code)', 'The CODE of the Batch must be unique!')]
_defaults = {
'state': 'planned',
}
how should I do that? Please Help me to do this...
To enforce data integrity, odoo support two types of constraints: SQL and Python.
SQL constraints are added to the table definition in the database and implemented by PostgreSQL. They are defined using the class attribute _sql_constraints. It is a list of tuples with the constraint identifier name, the SQL for the constraint, and the error message to use.
Python Constraint
In v7 api,
_constraint is a collection of list of tuples.
Tuple contains three parameters,
_constraint will raise the validation message if the condition returns False on create/update record.
Just add constraints for that,
def _check_date(self, cr, uid, vals, context=None):
for obj in self.browse(cr, uid, ids):
start_date = obj.start_date
end_date = obj.end_date
if start_date and end_date:
DATETIME_FORMAT = "%Y-%m-%d" ## Set your date format here
from_dt = datetime.datetime.strptime(start_date, DATETIME_FORMAT)
to_dt = datetime.datetime.strptime(end_date, DATETIME_FORMAT)
if to_dt < from_dt:
return False
return True
_constraints = [
(_check_date, 'Your Message!', ['start_date','end_date']),
]
In v8 api,
@api.constrains
This decorator will ensure that decorated function will be called on create, write, unlink operation. If a constraint is met the function should raise a openerp.exceptions.Warning with appropriate message.
@api.multi
@api.constrains('start_date','end_date')
def _check_date(self):
for obj in self:
start_date = obj.start_date
end_date = obj.end_date
if start_date and end_date:
DATETIME_FORMAT = "%Y-%m-%d" ## Set your date format here
from_dt = datetime.datetime.strptime(start_date, DATETIME_FORMAT)
to_dt = datetime.datetime.strptime(end_date, DATETIME_FORMAT)
if to_dt < from_dt:
#raise your exception
If you want to change the existing constraints, it can be done by using inheritance see here