Skip to Content
Menu
This question has been flagged
2 Replies
2396 Views

I want to prevent the creation of a record in the database if the first condition is met what should i return in that case.

Thanks in advance everyone

def create(self,vals):
elv=self.env['gbv_elv'].browse(vals.get('eleveur'))
for verif in elv.verif:
last=verif
if((datetime.today().date())-last.date).days < 180:
print('hamborg')
else:
print('no hamborg')
print(((datetime.today().date()) - last.date).days)
ok=vals.get('ok')
elv.last_date=datetime.today()
if ok:
elv.rate='high'
else:
elv.rate='low'
return super(verifacation,self).create(vals)


Avatar
Discard
Best Answer
class my_class(models.Model)

#...

a = 1 #integer condition example

@api.mode

def create(self,vals) : 

​if self.a == 1 : 

​raise ValidationError('Your message here')

​else : 

​result = super(my_class,self).create(vals)

	​return result
Avatar
Discard

for your code
def create(self,vals):
elv=self.env['gbv_elv'].browse(vals.get('eleveur'))
for verif in elv.verif:
last=verif
if((datetime.today().date())-last.date).days < 180:
#this will prevent creating the record , and show a pop up with the message inside
raise ValidationError('Date Error or something')
else:
print('no hamborg')
print(((datetime.today().date()) - last.date).days)
ok=vals.get('ok')
elv.last_date=datetime.today()
if ok:
elv.rate='high'
else:
elv.rate='low'
return super(verifacation,self).create(vals)

Best Answer

Hi,

If you need to restrict the creation and notify the user why the creation is blocked, you can raise validation error when the condition is met.

Inside the if condition, return something like below:

raise ValidationError(_('You cannot create record now.'))

Thanks

Avatar
Discard
Related Posts Replies Views Activity
1
Jan 23
4945
1
Dec 23
2522
2
Aug 23
2233
1
Dec 23
3910
1
Jul 23
2821