Help

2

How to make ir.attachment mandatory and raise error when creating new leave request?

Avatar
Muhammad Anees

Hii,

We want to make sure that when an employee creating a new Leave Request on Leaves App, the attachment (document, pdf) should be selected/attached. 

Attachment option activates once Leave Request is saved. 

So before Leave is Confirm, we want to raise message "Please select attachment before confirm leave request."

How we can do it in Python in inherited class or some other recommended way?

Odoo 12.

Thanks,

Avatar
Discard
2 Answers
1
Avatar
Muhammad Anees
Best Answer

Here I got it working..

class HrLeave(models.Model):
    _inherit = 'hr.leave'
 @api.multi
def action_confirm(self): record = super(HrLeave, self).action_confirm() if self.message_main_attachment_id.id is False and self.holiday_status_id.time_type == 'full_day_training': raise ValidationError("Please attach document for leave type: %s" % self.holiday_status_id.name) return record ​

Can we use record to get fields like record['message_main_attachment_id.id'] or record['name']?

But with self it ok.

1 Comment
Avatar
Discard
Avatar
Odoo Ninja
-

Technically the same is answered below

1
Avatar
Anu
Best Answer

Hi,

   You can raise an exception if an attachment field is not set while validating,try the code below

if not attachment_field:
raise exceptions.UserError(_("Please add attachment"))

     you can override the create method or any other method and check this     

Avatar
Discard