تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
2 الردود
2395 أدوات العرض

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
يناير 23
4944
1
ديسمبر 23
2521
2
أغسطس 23
2233
1
ديسمبر 23
3910
1
يوليو 23
2820