This question has been flagged
2 Replies
3245 Views

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.

Avatar
Discard
Author

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()

Best Answer

Create an Automated Action?




Avatar
Discard
Author

Thanks for the reply, but i already solved it, my problem was i had to overwrite unlink in product.template also

Best Answer

Hi,

In the database there might not be a product with the given external id, mymodule.myproductid , So update your code like this,

@api.multi
def unlink(self):
product_id = False
product = self.env.ref('mymodule.myproductid')
if product:
product_id = product.id
if product_id:
for rec in self:
if rec.id == product_id:
raise UserError('You shall not unlink!!')
return super(ProductProduct, self).unlink()

Thanks

Avatar
Discard
Author

No, that ID should exists, if not the product is already been deleted