Help

欢迎!

该社区面向专业人士和我们产品和服务的爱好者。
分享和讨论最好的内容和新的营销理念,建立您的专业形象,一起成为更好的营销人员。


0

When to use eval in XML fields?

Avatar
odoo
Avatar
Discard
1 Answer
0
Avatar
odoo
Best Answer

Odoo document link for eval and ref attribute are:-

https://doc.odoo.com/6.0/developer/2_6_views_events/views/design_element/

https://doc.odoo.com/6.0/developer/5_16_data_serialization/xml_serialization/

eval attribute

The eval attribute evaluate its content as if it was Python code. This allows you to define values that are not strings.

Normally, content inside <field> tags are always evaluated as strings.

Example 1:

<field name="value">2.3</field>

This will evaluate to the string '2.3' and not the float 2.3

Example 2:

<field name="value">False</field>

This will evaluate to the string 'False' and not the boolean False

If you want to evaluate the value to a float, a boolean or another type, except string, you need to use the eval attribute:

<field name="value" eval="2.3" /> <field name="value" eval="False" />

ref attribute

The ref attribute allows to fill relations between the records :

<field name="company_id" ref="main_company"/>

The``company_id`` field is a many-to-one relation from the user object to the company object, and main_company is the id of to associate.

 

In xml using eval and ref  attribute to assign the value for field and create new master record:-

Example:- addons/product/product_view.xml

     <record id="product_uom_categ_vol" model="product.uom.categ">
            <field name="name">Volume</field>
        </record>

        <record id="product_uom_gal" model="product.uom">
            <field name="name">gal(s)</field>
            <field name="category_id" ref="product_uom_categ_vol"/>
            <field name="factor_inv" eval="3.78541"/>

            <field name="uom_type">bigger</field>
        </record>

 

3 Comments
Avatar
Discard
Avatar
odoo
-

Thanks. I see. But then, this case: is redundant. You do not need "eval" if you are already using "ref", isn't it?

Avatar
odoo
-

yes either use eval or ref. using ref assign xml record ID value and using eval assign normal value

Avatar
odoo
-

I did note that in some cases eval="ref(...)" gives no error, but using ref="" does.

It might be that ref called from eval is no mandatory to exist.