Skip to Content
Menu
This question has been flagged
1 Reply
3396 Views

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
Discard
Best Answer

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
Discard
Related Posts Replies Views Activity
1
Oct 23
359
2
Oct 23
667
2
Aug 23
1957
4
Aug 23
18243
3
Oct 22
8898