0
votes

How to add sql constrain of date only from field datetime

date = fields.Datetime('Date', store=True, default=fields.Datetime.now, )

1

1 Answers

0
votes

In this case, you don't need sql restrictions. A better option is to extend the field and change its data type to Date:

class NewClass(models.Model):
    _inherit = "original.class"
    ...
    date = fields.Date('Date', store=True, default=fields.Date.today())

If you don't want to do this, use instead the constraints decorator over your field:

@api.constrains('date')
def check_data_type(self):
    if not condition:
        raise ValidationError("Error!")

In any case, if you still want to add a sql constraints you can use this in your model:

class YourClass(models.Model):
    _name = "your.class"
    ...
    _sql_constraints = [('date', 'CHECK (your check)', 'Data no pass the check')]

I hope this helps!