Skip to Content
Menu
This question has been flagged
4 Replies
60854 Views

Hello,

I am having trouble using XPath to do anything with views. I've read a lot on StackOverflow and Odoo's official documentation about XPath and view inheritance, but I don't understand how to use it because it never seems to work for me. i am using the inventory app. If I use the following in my view, it correctly makes the list_price field invisible

<odoo>
    <data>
        <record id="view_kit_product_form" model="ir.ui.view">
            <field name="name">kit_product_form</field>
            <field name="model">product.template</field>
            <field name="inherit_id" ref="product.product_template_form_view"/>
                     <field name="arch" type="xml">                             
                        <field name="list_price" position="attributes">
                            <attribute name="invisible">True</attribute>
                        </field>                    
                     </field>
        </record>
    </data>
</odoo>

but replacing

<field name="list_price" position="attributes">
      <attribute name="invisible">True</attribute>
</field>

with

<xpath expr="/form/sheet/notebook/page/group/group/field[name='list_price']" position="attributes">
    <attribute name="invisible">True</attribute>
</xpath>

or

<xpath expr="//field[@name='list_price']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>

or

<xpath expr="field[@name='list_price']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>

will always cause an error which creates a server error and tells me this:

Element '<xpath expr="field[@name=list_price]">' cannot be located in parent view.

In fact, trying to do anything with XPath has given me this error, and I don't understand why.

Shouldn't

<xpath expr="field[@name='list_price']"/>

Do the same thing as

<field name="list_price"/>

?

Can anybody tell me what I'm doing wrong? Or if there are any better resources which explain how to use XPath with Odoo and view inheritance, I would really appreciate it if you could tell me about them! I've seen people say you can use XPath with a view to even hide pages in a notebook in a form, but trying to do that also gives me an error.

Any help would be greatly appreciated

Avatar
Discard

Hi, try this <xpath expr="//page[@name='general_information']/field[@name='list_price']" position="attributes">

Author

That gives the same error:

Element '<xpath expr="//page[@name='general_information']/field[@name='list_price']">' cannot be located in parent view

Shouldn't

<xpath expr="field[@name='list_price']"/>

Do the same thing as

<field name="list_price"/>

?

It shouldn't as you're telling the machine to search on the top level only. Instead, it should be:

<xpath expr="//field[@name='list_price']" position="attributes">

...

</xpath>

But my guess is that you just have missed leading '//' for the second time. I tried your code, it all works fine except for the one without leading '//'. You should probably try checking the spelling.

Author

Thank you thompsonn! You are right, and that is a helpful explanation that it is only searching the top level if you leave out "//"

Posted it as an answer, please don't hesitate to mark it as accepted :) Thanks

Best Answer
Shouldn't
<xpath expr="field[@name='list_price']"/>
Do the same thing as
<field name="list_price"/>
?

It shouldn't as you're telling the machine to search on the top level only. Instead, it should be:

<xpath expr="//field[@name='list_price']" position="attributes">
...
</xpath>

But my guess is that you just have missed leading '//' for the second time. I tried your code, it all works fine except for the one without leading '//'. You should probably try checking the spelling.

Avatar
Discard
Best Answer

Can anybody tell me what I'm doing wrong? Or if there are any better resources which explain how to use XPath with Odoo and view inheritance...
Yes why not I found some useful resources about XPath and inheritance in views and models.
http://learnopenerp.blogspot.com/2018/01/inheritance-in-models-and-views.html
Have a look into hope its helpful for you and others.
Thanks

Avatar
Discard
Best Answer

To clearify here, '//field['foofield']' won't work if their ain't any field of name foofield, you'll have to look for element props. If you wanna search for an element in table, use //table['@name='footable']. good luck.

Avatar
Discard
Related Posts Replies Views Activity
2
May 24
902
4
Sep 20
4834
0
Aug 21
2405
5
Aug 19
8020
1
Jan 18
6076