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

I try whit that, but don't work:

@api.multi
def write(self, values):
if self.total_desglose_store > self.plan_year_store:
raise UserError(
_('El total del desglose por empresa "%s" no puede ser mayor que el Plan Asignado "%s"') % (
self.total_desglose_store, self.plan_year_store))
return super(df_en_cda001_aprov_comp, self).write(values)
الصورة الرمزية
إهمال
أفضل إجابة

Hi,

You can use api.constrains for this, see this:

@api.constrains('total_desglose_store', 'plan_year_store')
def check_age(self):
for rec in self:
if rec.total_desglose_store < rec.plan_year_store:
raise ValidationError(_('Error Message..!'))

Video: How To Add Constrains For A Field in Odoo12

Thanks

الصورة الرمزية
إهمال
الكاتب أفضل إجابة

Thank you very much for your answers, both have been very useful

الصورة الرمزية
إهمال
أفضل إجابة

As Niyas mentioned, api.constrains is the correct way to do this. However, looking at why your code didn't work may help you learn. There were three main issues with the code:

  1. Doing you check before calling super. This means that you are actually checking the values before they are updated. You need to call after super for vals to be applied.

  2. Doing a write hook only. This will not be called on the creation of a record, so a create hook would be required.

  3. Calling self.<field_name> in a function that can take multiple records. This will break if write was called on two or more records. You need to loop over the records in self.

This code will work, but you should still take Niyas' approach as it is the standard way to do this:

def write(self, vals):
# Call super
res = super(df_en_cda001_aprov_com, self).write(vals)
# Loop over records
for rec in self:
if rec.total_desglose_store < rec.plan_year_store:
raise ValidationError(_('Error'))
return res
الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
مايو 25
398
2
نوفمبر 22
6138
1
سبتمبر 15
4150
1
أبريل 24
1292
0
مارس 24
728