This question has been flagged
2 Replies
4744 Views

Hi,

I've been struggling for some time now with the following function, which belongs to the product.category class. I understand what it's trying to accomplish, but I don't understand the whole code:

    def name_get(self, cr, uid, ids, context=None):
        if isinstance(ids, (list, tuple)) and not len(ids):
            return []
        if isinstance(ids, (long, int)):
            ids = [ids]
        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
        res = []
        for record in reads:
            name = record['name']
            if record['parent_id']:
                name = record['parent_id'][1]+' / '+name
            res.append((record['id'], name))
        return res

This is liked I guess to the following question:

https://www.odoo.com/forum/help-1/question/product-category-parent-id-parent-left-parent-right-30007

I understand the concept but I don't get how everything is linked together, specially the next sentence:

name = record['parent_id'][1]+' / '+name

It may seem a basic question but I'd appreciate any help on this.

Thanks in advance.

Avatar
Discard
Best Answer

The line does the following

if there is a parent id  then it will change the name variable

the new value has three elements which are chained together:

on the parent id record value of [1] = second entry, first entry would be "[0]"
(first is probably id, so second might probably be name)

th character "/" with spaces " / "

the orginial value of name

this then might become a string like "Parent Name / Child Name"

 

A good way to understand such functions is to observe them when they are working. Therefore I can recommend you pycharm community edition and then launch the server in debugging mode, put some breakpoints here and there and look what the expressions contain at runtime. It's a bit of work to set everything up, but it will boost your learning curve extremely! (There are other tools available to use as IDE, yet this is my personal choice, and I like it - it's freeware)

 

Avatar
Discard
Author Best Answer

Thank you so much David, I'll follow your advice gladly.

Regards,

Manuel.

Avatar
Discard