Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
2404 Переглядів

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)


Аватар
Відмінити
Найкраща відповідь
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
Аватар
Відмінити

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)

Найкраща відповідь

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

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
1
січ. 23
4947
1
груд. 23
2526
2
серп. 23
2234
1
груд. 23
3911
1
лип. 23
2824