Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
4479 Widoki

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.

Awatar
Odrzuć
Autor

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

Najlepsza odpowiedź

Create an Automated Action?




Awatar
Odrzuć
Autor

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

Najlepsza odpowiedź

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

Awatar
Odrzuć
Autor

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

Powiązane posty Odpowiedzi Widoki Czynność
2
sty 21
6418
1
cze 20
5030
2
mar 20
5357
1
sty 20
3094
1
paź 18
5387