Hi, i want to restric a product i have inserted in database from my module, the product is mandatory to be used with the module, so i wan't to lock the unlink method only for that product, i tryed overwriting unlink method and getting the product by xml_id and comparing with the current one i am deleting:
@api.multi
def unlink(self):
product_id = self.env.ref('mymodule.myproductid').id
for product in self:
if product.id == product_id:
raise UserError('You shall not unlink!!')
return super(ProductProduct, self).unlink()
But i'm getting raise ValueError('No record found for unique ID %s. It may have been deleted.' % (xmlid))
Im using the first line to get the product in another part of my code and is working fine, so it's seem the unlink method triggers delete even b4 i call the super method or something.
Just if someone else face this problem or need i leave my solution here:
from odoo import api, models
from odoo.exceptions import UserError
class ProductTemplate(models.Model):
_inherit = 'product.template'
@api.multi
def unlink(self):
product = self.env.ref('mymodule.myproductxmlid')
template_id = product.product_tmpl_id.id
for rec in self:
if rec.id == template_id:
raise UserError('You shall not unlink!!')
return super(ProductTemplate, self).unlink()
class ProductProduct(models.Model):
_inherit = 'product.product'
@api.multi
def unlink(self):
product_id = self.env.ref('mymodule.myproductxmlid').id
for rec in self:
if rec.id == product_id:
raise UserError('You shall not unlink!!')
return super(ProductProduct, self).unlink()