Sunday, September 13, 2020

overwrite unlink function in odoo

ODOO


 

unlink() function

To explain how we can overwrite the unlink() function in odoo we must understand some points

  • any object in odoo has it's own unlink() function which is enable the user to delete any record from this object with out any condition
  • we overwrite the unlink() function when we want to put some conditions on to delete records from the object , or if we want to make the any record unable to be deleted when it created 
Now let us give an example and let us say that we have an object by name "sas.admission" with class name "sas_admission", and in this object we have state field as 

state = fields.Selection([('draft','Draft'),('request','Request'),('done','Done')], string="State", default='draft')

and we want to make any record on this object can to deleted only if it's state is "draft" so we can overwrite the unlink() function as bellow 

@api.multi
    def unlink(self):
        """
        overwrite the unlink function to make user can delete the record if it's state is draft only
        """
        if self.state != 'draft':
            raise ValidationError(_('You cannot delete this record because it state is not draft'))
        return super(sas_admission, self).unlink()

the validation error message will look like 



* don't forget to import ValidationError on your class as bellow 
 
    from odoo.exceptions import ValidationError

No comments:

Post a Comment

Odoo Invoice Qr code issues

There are two main issues must of us facing with the QR code in Odoo invoice & these issues are 1/ QR code displayed as broken image w...