Skip to Content
Menu
This question has been flagged
3 Replies
10409 Views

i have selected the start date. this selected date how will i validate/check to current date?

Avatar
Discard
Best Answer

Hi, you can use the below code to validate...

from datetime import datetime

start = datetime(2015, 1, 1)
current = datetime.now()
print current
print start < current

You can use current date as default

'today_date' : fields.date.context_today
Avatar
Discard
Best Answer

Hi,

You can try the following code.

   @api.constrains('start_date')
def _check_start_date(self):
for rec in self:
if rec.start_date raise ValidationError('start date cannot be in past')

Regards

Avatar
Discard
Best Answer

Hi,

You can do something like below:

def _check_dates(self, cr, uid, ids, context=None):
    if context == None:
        context = {}
    obj_task = self.browse(cr, uid, ids[0], context=context)
    start = obj_task.start_date or False
    end = time.strftime('%Y-%m-%d')
    if start and end :
        if start < end:
            return False
    return True

_constraints = [
    (_check_dates, 'Error ! Start Date must be greater then Current Date', ['start_date'])
]

You can also apply this method on onchange method of start_date.

Hope this may help you.

Avatar
Discard