python - openERP function for validate date range -


i have 2 fields in module,(start_date , end_date). want validate date range end_date must greater start_date , display error message "end date should greater start date" 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 greater start date"))     return true      _sql_constraints = [('code', 'unique (code)', 'the code of batch must unique!')]      _defaults = {     'state': 'planned',     } 

how should that? please me this...

to enforce data integrity, odoo support 2 types of constraints: sql , python.

sql constraints added table definition in database , implemented postgresql. defined using class attribute _sql_constraints. list of tuples constraint identifier name, sql constraint, , error message use.

python constraint

in v7 api,

_constraint collection of list of tuples.

tuple contains 3 parameters,

  1. method name (where actual logic coded)
  2. the validation message (message want show user)
  3. list of fields (fields want apply constraint)

_constraint raise validation message if condition returns false on create/update record.

just add constraints that,

def _check_date(self, cr, uid, vals, context=none):     obj in self.browse(cr, uid, ids):         start_date = obj.start_date         end_date = obj.end_date          if start_date , end_date:             datetime_format = "%y-%m-%d"  ## set 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 ensure decorated function called on create, write, unlink operation. if constraint met function should raise openerp.exceptions.warning appropriate message.

@api.multi @api.constrains('start_date','end_date') def _check_date(self):     obj in self:         start_date = obj.start_date         end_date = obj.end_date          if start_date , end_date:             datetime_format = "%y-%m-%d"  ## set 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 exception 

if want change existing constraints, can done using inheritance see here


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -