Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
2 Besvarelser
2399 Visninger

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
Kassér
Bedste svar
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
Kassér

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)

Bedste svar

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
Kassér
Related Posts Besvarelser Visninger Aktivitet
1
jan. 23
4946
1
dec. 23
2525
2
aug. 23
2233
1
dec. 23
3911
1
jul. 23
2824