Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
4346 Vistas

Hello. I have a method that works properly only if called from a button. But when its triggered by onchange it doesnt work properly

    @api.onchange('categ_id')
    def compute_categories(self):
        categ_ids = []
        products = self.env['product.product'].search([])
        for product in products:
            category = product.categ_id
            while category.parent_id:
                categ_ids.append(category.id)
                category = category.parent_id
            categ_ids.append(category.id)
            product.write({'category_ids': [(60, categ_ids)]})
            categ_ids = []

 

I think the problem is when i use onchange self looks like this:  product.product(<odoo.models.NewId object at 0x7fa8abb809d0>,)


And when I call my methdo with button click self looks like this: product.product(1,)


I tried to do: origin = self._origin but did not work

Avatar
Descartar
Mejor respuesta

Hello David,

I have tried with your code,and it has been working in my system.

another way to do this please try with compute method,you change the category field

and base on that you can compute categories and store it.


category_ids = fields.Many2many('product.category',compute='compute_categories',store=True)


@api.depends('categ_id')

    def compute_categories(self):

    for rec in self:

        categ_ids = []

        products = self.env['product.product'].search([])

        for product in products:

            category = product.categ_id

            while category.parent_id:

                categ_ids.append(category.id)

                category = category.parent_id

            categ_ids.append(category.id)

            product.write({'category_ids': [(6, 0, categ_ids)]})

            categ_ids = []

Regards,




Email:      odoo@aktivsoftware.com  

Skype: kalpeshmaheshwari

   

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
oct 23
1776
2
oct 23
2019
2
ago 23
3994
4
ago 23
20280
3
oct 22
10762