Skip to Content
Menu
This question has been flagged
2 Replies
4702 Views

model is 'product.category ', want to inhereit and add a field 'level' to add the levels to find which level record i want.

we knew that by default (as recommended) that first entry 'All' here should present to start the parent child relations.

when user create a new category and select All as its parent, we knew that its level is 1 and we can add 2, programatically it should be parent level + 1 ... so we can add levels of any depth.

i don't know how to code it, so please help me for the below onchange function (or any other better approach, please specify), modify it to achieve what i wants to have.

@api.onchange(parent_id):

def _onchange_parent_id(self):

  find the level of parent here...

  self.level = parent_level + 1

regards


Avatar
Discard
Best Answer

Hello SmithJohn45,

Suppose we have a product category hierarchy as below


A > B > C > D


and we need to find the level for the category "C" with the code (the answer is 3)

So to find the level for the category, here is the method that you need to write:


def find_level_of_category(self, catg_id):

    level = 1

    if catg_id.parent_id:

        parent_id = catg_id.parent_id

        while (parent_id):

            level += 1

            parent_id = parent_id.parent_id

        return level

    return level



You can simply call this method to your onchange, you need to pass "self.parent_id" as a parameter into the above method, so it will give you the level of the "self.parent_id".


Thanks!

For more information Contact us:- https://kanakinfosystems.com/odoo-development-services

Avatar
Discard
Author

thank you @Neha Sharma for help :), will implement and give feedback here.

Author

@Neha Sharma, please check, if it is ok or i made mistake(s)

from odoo import api, fields, models

class ProductCateg(models.Model):

_inherit = 'product.category'

level = fields.Integer(string='Level')

@api.onchange('parent_id')

def find_level_of_category(self):

levels = 1

if self.parent_id:

parent_id = self.parent_id

while (parent_id):

levels += 1

parent_id = parent_id.parent_id

self.level = levels

regards

# -*- coding: utf-8 -*-

from odoo import api, fields, models

class ProductCateg(models.Model):

_inherit = 'product.category'

level = fields.Integer(string='Level')

@api.onchange('parent_id')

def onchange_parent_id(self):

if self.parent_id:

level = self.find_level_of_category()

self.level = level

def find_level_of_category(self):

level = 1

if self.parent_id:

parent_id = self.parent_id

while (parent_id):

level += 1

parent_id = parent_id.parent_id

return level

return level

Author

@Neha Sharma, implemented the code for model, updated manually level column for All to 1 and All / Electronics to 2, from UI created a record, choose All / Electronics as its parent, but level not updated with required value. ( i have copy/pasted your last code as it is, indent with pycharm option and restart server, upgraded module and then checked ) anything else i missed ?

Can you share a screenshot of your pycharm code?

Author

can't post screenshot here, so i have to use Answer portion here...

Author

@Neha Sharma, i have added screenshot, please check.

Okay I will attach .py and xml file and video or flow maybe it helps you


On Fri, 29 Jan 2021 at 15:47, SmithJohn45 <oralover2006@gmail.com> wrote:

@Neha Sharma, i have added screenshot, please check.

Sent by Odoo S.A. using Odoo.

i have sent a mail to you with attached .py and xml file along with video maybe it works

Author

thanks @Neha Sharma for your struggle to help me, i have seen in my gmail account for your email but not yet received, also checked in Spam, may be sometimes take time, will check after 15 minutes. thanks again :)

Ok, no problem. 

On Fri, 29 Jan 2021 at 18:15, SmithJohn45 <oralover2006@gmail.com> wrote:

thanks @Neha Sharma for your struggle to help me, i have seen in my gmail account for your email but not yet received, also checked in Spam, may be sometimes take time, will check after 15 minutes. thanks again :)

Sent by Odoo S.A. using Odoo.

Have you received my mail?

On Fri, 29 Jan, 2021, 6:15 PM SmithJohn45, <oralover2006@gmail.com> wrote:

thanks @Neha Sharma for your struggle to help me, i have seen in my gmail account for your email but not yet received, also checked in Spam, may be sometimes take time, will check after 15 minutes. thanks again :)

Sent by Odoo S.A. using Odoo.

Author

@Neha Sharm, no, not yet received email, don't know why, even checked Spam and also Trash may be i accidently deleted but failed to find the email. what is the Subject of email? and is it through Odoo platform?

how i can request a feature in forum, there is no notification here within forum if someone Answer or Comment... user should check the email or On email's notification, but it is not possible for us especially when we are on Client.

okay, I will be sent you a mail again with some attachments of the .py and XML file and video flow.


On Sat, 30 Jan 2021 at 09:22, SmithJohn45 <oralover2006@gmail.com> wrote:

@Neha Sharm, no, not yet received email, don't know why, even checked Spam and also Trash may be i accidently deleted but failed to find the email. what is the Subject of email? and is it through Odoo platform?

how i can request a feature in forum, there is no notification here within forum if someone Answer or Comment... user should check the email or On email's notification, but it is not possible for us especially when we are on Client.

Sent by Odoo S.A. using Odoo.

I already sent you mail again if you find it then please reply to my mail.
Thanks...

On Mon, 1 Feb 2021 at 16:34, Neha Sharma <nsa@kanakinfosystems.com> wrote:
okay, I will be sent you a mail again with some attachments of the .py and XML file and video flow.


On Sat, 30 Jan 2021 at 09:22, SmithJohn45 <oralover2006@gmail.com> wrote:

@Neha Sharm, no, not yet received email, don't know why, even checked Spam and also Trash may be i accidently deleted but failed to find the email. what is the Subject of email? and is it through Odoo platform?

how i can request a feature in forum, there is no notification here within forum if someone Answer or Comment... user should check the email or On email's notification, but it is not possible for us especially when we are on Client.

Sent by Odoo S.A. using Odoo.

Author Best Answer

@Neha Sharma, screenshot is here as instructed. (already mentioned in my opening post that "i don't now how to code it" so, it is copy pasted as you did it for me)


Avatar
Discard

Where did you paste it?

Related Posts Replies Views Activity
1
Jul 23
4362
1
Mar 15
6985
1
Apr 24
287
0
Nov 21
1271
0
May 17
3362