Search code examples
pythonodoo

How can I define constraints for a date field in odoo?


I have a model in odoo with a fields.Date attribute (expiration_date). How can I add a constraint to that field to check that any date entered by the user must be after a sertain date (like january 2016)


Solution

  • You have to use user defined constraint to achieve this. Suppose, that certain date is Today.

    @api.constrains('expiration_date')
    def _check_expiration_date(self):
        if self.expiration_date <= datetime.today():
            raise ValidationError('Expiration date must be after today.')