Search code examples
datepickerodoodatetimepicker

Disable previous dates in Odoo datepicker


I want to restrict the user selecting previous date from the date picker of odoo-8. Please give me how to disable previous dates in odoo datepicker


Solution

  • There's a module for that https://apps.openerp.com/apps/modules/8.0/web_widget_datepicker_options/

    If you have a date field named current_date

    <field name="current_date" />
    

    After installing the module, just add the option for the jquery datepicker minDate and set it to 0 like this

    <field name="current_date" options="{'datepicker':{'minDate': 0}}"/>
    

    Screenshot Screenshot

    I previously did this by setting an onchange on the field that'll be triggered every time the field is changed, and in the onchange you can convert the date to a python date (with odoo's default time format) and compare it to the current date

    from datetime import datetime
    from openerp import api
    from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
    from openerp.exceptions import Warning
    
    @api.onchange('current_date')
    def onchange_date(self):
        if datetime.strptime(self.current_date, DEFAULT_SERVER_DATE_FORMAT).date() < datetime.now().date():
            raise warning('Please select a date equal/or greater than the current date')
            return False
        return my_date