phani
phani

Reputation: 131

@api.constrains is not working properly

In the 'project.project' model I wrote a function to validate 'start Date' and 'end date' for this I used onchange function.The function is working and giving warning but record is creating. Actually if there is any error means record cannot be saved because I am using @api.constrains below is my code.

py.code:

@api.onchange('date','date_start')
@api.constrains('date','date_start')
def cheking_field_date(self):
    self.t1 = self.date_start 
    self.t2 = self.date
    if self.t2 == False:
        pass
    else:   
        if str(self.t1) > str(self.t2):
            raise Warning('The Deadline Date is Invalid')
        else:
            pass  

But When I was Editing the record if any change in date field means than everything is fine(i.e. validation problem in 'start date' and 'end date').At that time the record is not saving.Why this type problem is coming can any one help me please.

Upvotes: 3

Views: 1728

Answers (1)

Bhavesh Odedra
Bhavesh Odedra

Reputation: 11143

You need to take care of following points:

  • No need to use @api.onchange
  • Remove unnecessary variables.
  • Write only condition that you want to raise warning.

Try with following code.

@api.one
@api.constrains('date','date_start')
def cheking_field_date(self):
    if self.date_start and self.date:
        if self.date_start > self.date:
            raise Warning('The Deadline Date is Invalid')

Upvotes: 5

Related Questions