Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
2 Vastaukset
3177 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Tekijä

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.

Tekijä

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

Tekijä Paras vastaus

The version of Odoo is 17

Avatar
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
5
lokak. 24
15130
3
elok. 22
11665
2
helmik. 19
3830
2
kesäk. 16
5841
3
jouluk. 22
7159