Se rendre au contenu
Menu
Cette question a été signalée
2 Réponses
2398 Vues

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
Ignorer
Meilleure réponse
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
Ignorer

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)

Meilleure réponse

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
Ignorer
Publications associées Réponses Vues Activité
1
janv. 23
4946
1
déc. 23
2525
2
août 23
2233
1
déc. 23
3911
1
juil. 23
2824