Ernesto Ruiz
Ernesto Ruiz

Reputation: 820

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)

Upvotes: 0

Views: 846

Answers (1)

Purvi Bhatt
Purvi Bhatt

Reputation: 126

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.')

Upvotes: 2

Related Questions