This question has been flagged
8 Replies
6327 Views

I need to create a name for my products; this name will concatenate the values found into 2 or 3 others that already exist: product.name (standard Odoo) + personalized fields.

A solution was given for a different environment: see 


Merging fields into one

Specifically the Saas environment does't allow to create a new field with the function option.

Any direction will be appreciated

UPDATE :

(I tested the following configuration having created 2 fields x_nomcomplet and x_auteur type char on modèle product template modèle d'article)


remove previous server action and create 2 new records with values for fields of server action :


first (for field Action name you can choose what you want):

Action name : Nom complet article

Base Model : Modèle d'article

Action To Do : Execute python code

condition : True

Sequence : 5

notebook python code, add following code:


context = context or {}

for record in self.browse(cr, uid, context.get('active_ids', []), context=context):

    value = ""

    for val in [record.name, record.x_auteur]:

        if val:

            value += "%s " % val

    pool.get('product.template').write(cr, uid, record.id, {'x_nomcomplet': value[:-1]}, context=context)


at the top right of the form push button "Add in the More menu" (will add buttons to tree and form of the menu product)


second (for field Action name you can choose what you want):

Action name : Nom complet variante

Base Model : Article

Action To Do : Execute python code

condition : True

Sequence : 5

notebook python code, add following code:


context = context or {}

for record in self.browse(cr, uid, context.get('active_ids', []), context=context):

    value = ""

    for val in [record.product_tmpl_id.name, record.product_tmpl_id.x_auteur]:

        if val:

            value += "%s " % val

pool.get('product.template').write(cr, uid, record.product_tmpl_id.id, {'x_nomcomplet': value[:-1]}, context=context)


at the top right of the form push button "Add in the More menu" (will add buttons to tree and form of the menu product variant)


test in menus product and variant

Avatar
Discard
Author

@Cyril: yes I have access to this menu and lso to technical features.

Author

I am interested by an automatic behaviour which allows to store the result of the concatenation of 2 or 3 existing fields into a new one. This new field will replace the name of the variants in every views and forms where the variant name is used.

if you check def name_get() in product.py you will see name is concatenated with default_code, you can add any other field with '+' symbol. or are you looking for a new field storing this concatenated value in database?

@John, in mode Odoo online SAAS, no possibility to modify python code or add a new module, to create a field function or related, hard ^^

Author

@ John. From your comment, one can deduct that the concatenation is hard coded. This may appear as an conception error regarding the required flexibility. Many questions on this forum are showing the need to customise!

Best Answer

only solution I know for SAAS odoo version to execute new python code is to use a server action, which will add button "Update name"  (see after) to the form in mode edit at the top menu button "More" AND list view at the top menu button "More" (to perform execution to all selected lines) of the object define in your server action, buttons which will execute your python code.


my example :

I will set to the field name of product template object (menu product),  computation of fields type and default code (verify you have values for that fields else "/" will be set, because this field is required, even if there is always a value for field type, but perhaps this will be not the case for your need)


go to menu configuration/Technical/Actions/Server Actions

create a new record with values for fields of server action :

Action name : Update name

Base Model : Product template

Action To Do : Execute python code

condition : True

Sequence : 5

notebook python code, add following code:


context = context or {}

for record in self.browse(cr, uid, context.get('active_ids', []), context=context):

    name = ""

    for val in [record.type, record.default_code]:

        if val:

            name += "%s " % val

    if name:

        name = name[:-1]

    elif not record.name:

        name = "/"

    pool.get('product.template').write(cr, uid, record.id, {'name': name}, context=context)



at the top right of the server action form push button "Add in the More menu" (will add buttons to tree and form of the object)

save server action


go to menu sales/products/product variants, choose a product, open form in edit mode, at the top menu button More push "Update name", name of your product has changed as "value_field_type value_default_code" (update browser page if it was already opened)

enjoy !


bye

 


Avatar
Discard

Hi, I tested and updated code in your first post, follow instructions step by step, tested then should work for you too, bye

Author

@Cyril: many thanks; it works as expected; however -when the concatenation leads to a long string, then an error message warns that the length is too long for the field; and the field size cannot be changed because it is a base field.

Hi, change lenght of youy file x_nomcomplet in menu configuration/technical/structure de la base de données/champs, search/open form of field x_nomcomplet, in field "Taille" in the form, remove the value if there is one to have a varchar (dynamic lenght), bye

Author

Sorry Cyril; I had a highly busy period since you last advice and no time left to fine tune the issue as per your directions. I will do it in a few days. I have another issue to solve and I was thinking you - Cyril - could help me: I need to create a server action on the product. template object and adding it to the "more options" menu; this action will generate the Ean13 cusotmized value for a whole series of products; it needs to use the values of another field already present in the database and convert them into the EAN13. I know how to create the button itself but I am unable to code the python code to be added to the notebook. I have asked the question on this forum but no answer; therefore my call to you If you will. Regards

Best Answer

Thank you for this process I didn't know !

Avatar
Discard