Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
4516 Vizualizări

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.

Imagine profil
Abandonează
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()

Cel mai bun răspuns

Create an Automated Action?




Imagine profil
Abandonează
Autor

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

Cel mai bun răspuns

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

Imagine profil
Abandonează
Autor

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

Related Posts Răspunsuri Vizualizări Activitate
2
ian. 21
6534
1
iun. 20
5163
2
mar. 20
5444
1
ian. 20
3179
1
oct. 18
5476