Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
2960 Vistas

Dear community,


I need your help. I'm trying to change the string dynamically, the string is getting with value other field of the model. Is it posible?


For example

I have a field with name 'var1' and 'valuevar1', I want to get the value of the 'valuevar1 and put the value in the name of the other string.



Kind regards

Avatar
Descartar
Mejor respuesta

I'm not entirely sure what you want to achieve, but you can use a compute field for this (https://www.odoo.com/documentation/17.0/developer/tutorials/getting_started/09_compute_onchange.html). The code should look something like this:

var1 = fields.Char(compute="_compute_dynamic_string")

@api.depends('valuevar1')
def _compute_dynamic_string(self):
​for record in self:
​record.var1 = record.valuevar1
​return True

I hope this helps!

Avatar
Descartar
Autor

Dear Jort,

I want to change de name of the column of the view, I don't want to change the value of the field.

Exemple :

<field name="var1" string="valuevar1_field_value" />

Kind regards

In that case I misunderstood. Your use case is a bit more complicated, but you can check out the following blog post, which might help you further:

https://www.cybrosys.com/blog/how-to-dynamically-change-label-string

If you want real time updates you would have to use javascript for this, i.e., when the value of 'valuevar1' changes in the view then the field string should update as well.

Autor

Thanks Jort,

I have changed a little bit the code for it's running in odoo 17

the new code is :

@api.model
def _get_view(self, view_id=None, view_type='form', **options):
arch, view = super()._get_view(view_id, view_type, **options)
if view_type == 'form':
parameters = self.env['ir.config_parameter'].sudo()
name_var1 = parameters.get_param('app1.var1')
if (name_var1 != False):
for node in arch.xpath(
"//field[@name='var1']"
):
node.attrib['string'] = name_var1
return arch, view

Kind regards

Kind regard

Autor Mejor respuesta

The version of Odoo is 17

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
5
oct 24
14667
3
ago 22
11527
2
feb 19
3535
2
jun 16
5722
3
dic 22
7012