This question has been flagged
11 Replies
9374 Views

Trying to combine 3/4 fields into one in product.

For example i would like to create product name using fields I created. brand, model, size etc

So name = brand+model+size+... and paste it into <field name="name"/> then save.

How can i do it ?

Avatar
Discard
Author

Hi Empiro, still trying but all the time a'm getting some errors, once it is wrong indentation, once i fought it it gives me an error "Brand is not defined" however it is defined but in other module. so what should i do ? remove from my module and create new in this module ?

@robert If you have defined "model, brand and size" then directly use those fields. Use same name as what you declared. That will work.

Add the field prod_name in your view as invisible (update browser view after module update), I also modify my code to write in field name of class product template and not product product, please avoid to display email you get from odoo, thanks, let me know the result, bye

Best Answer


from openerp import models, api, fields

from openerp.tools import ustr


class ProductProduct(models.Model):

    _inherit = "product.product"


    brand = fields.Char('brand')

    model = fields.Char('model')

    size = fields.Char('size')

    prod_name = fields.Char(compute="_get_prod_name", string="Product Name")


    @api.one

    @api.depends("brand", "model", "size")

    def _get_prod_name(self):

        brand = self.brand or ''

        model = self.model or ''

        size = self.model or ''

        name = ustr(brand) + " " + ustr(model) + " " + ustr(size)

        self.product_tmpl_id.name = name

        self.prod_name = name

Avatar
Discard
Author

Hey Cyril, is it possible to add some conditions to it and for example change set of fields if required ? example: if itype = "type1": prod_name = brand + ' ' + model + ' ' + size if itype = "type2": prod_name = brand + ' ' + model + ' ' + condition if itype = "type3": prod_name = brand + ' ' + size + ' ' + connection

Author

I don't think is possible just like that. Field itype is declared by other module so do I have to create a new itype in this module ?

Author Best Answer

Guys, I can't make it work, I already have brand, model and size declared. Why whilst am trying install new module it tells me brand not declared, etc.  After that i've had a message regarding indentation, now i have: " can only concatenate tuple (not "str") to tuple"

Avatar
Discard
Author

So in this case all fields I have are char type.

Author

Great thnx Cyril, finally i got it work. Based on this example i can build something more ;) Thanx a lot.

Best Answer

@Emipro,@Cyril How would you do the equivalent solution for a Saas user who only have the developper mode.

For instance I can't create a new field with a type function, because this type is not proposed under the Saas environment.

Although I really need this capacity to combine fields to name the product with the name of its composer and opus title (music domain)!

Avatar
Discard

Hi, please open a new post for that, your case is not similar than this of Robert, contributors will try answer to your problem for your case (explain what configuration menus are available for saas mode => menu modules, company ... or display a picture with all menus extend, a lot of people like me don't use saas version) , bye

Best Answer

@Robert


For this, you can create a new field as of type "Function" i.e fields.function()

from openerp import models,api,fields

class product(models.Model):

    _inherit = "product.product"

    @api.one

    @api.depends("brand","model","size")

    def get_prod_name(self):

        for product in self:

            brand = product.brand_id.name or ''

            model = product.model_id.name or ''

            size = str(product.size)

            product.prod_name= brand + "_"+model+"_"+size


   prod_name = fields.Char(compute="get_prod_name", string="Product Name",store=True)


Here you declared a new field "prod_name" which is a compute field. One more thing, product.product model has a field "name_template" which generally stores the same value as the field "name" from product.template. So you need to decide here whether to take the new field "prod_name" or not. Here I treat brand_id, model_id as many2one fields.

Hope this helps !!.

Avatar
Discard