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

I would like to change the string representation of product_id, so that it is changed in all reports where 

<span t-field="o.product_id"/>

is called and in forms, where 

<field name="product_id" />

is used.

The string representation of product_id looks like productname (variantvalue) i would like the variantkey to this.

I have a java background and there it would be overriding the .toString() method of the product_id object. How do can i change this in python/odoo, so i do not have to make changes to each and every form/list/report .

Avatar
Discard

Search many2one filed by other than name: https://goo.gl/7PHhPP

Best Answer

Hi!

The string representation in these fields is generally determined by the function name_get() of the concerned entity. In your case this would be name_get() inside the ProductProduct class (here for Odoo 12).

You can override this function and then either completely build the names yourself or take the result from the inherited class and modify them. You might want to take a look at the original method first, since the function is not really short in the case of products. In general, overriding the method works like in any other case, but keep in mind that it always returns an array of tuples in the form of (<ID>, <name>), because it is called for multiple products at once.

If you need additional product attributes but want to utilize the default name construction, you could modify the original string with something like this:


from odoo import api, models

class ProductProduct(models.Model):

    _inherit = 'product.product'

    @api.multi
    def name_get(self):
        result = []
        for record in self:
            name = super(ProductProduct, record).name_get()[0][1]
            # TODO: modify the name as needed
            result.append((record.id, name))
return result


Alternatively, you could of course construct the names completely on your own or just fall back to the super implementation for products you do not need to modify. I hope this works for you and helps you with all the cases you have in mind.

Avatar
Discard
Author Best Answer

Thanks Marcel for your answer.

If i have a look at ProductProduct class (\odoo 12) i think it is line 467 that i want altered (although 477 looks similar)

 name = variant and "%s (%s)" % (product.name, variant) or product.name

to something like

 name = variant and "%s (%s: %s)" % (product.name, variant.attribute_id.name, variant) or product.name

But indeed, would i want to override that whole bunch of code ?

It baffles me that not more people want the AttributeName together with the AttributeValue in the product description.

Avatar
Discard