تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
65193 أدوات العرض

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

الصورة الرمزية
إهمال

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

الكاتب

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.

الكاتب

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

أفضل إجابة
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.

الصورة الرمزية
إهمال
أفضل إجابة

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

الصورة الرمزية
إهمال
أفضل إجابة

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.

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
2
مايو 24
2171
4
سبتمبر 20
5615
0
أغسطس 21
2991
5
أغسطس 19
8985
1
يناير 18
6863