How to add a field into an inherited view's tab/page? - odoo

How can I inherited a view to add a field into the "Sessions" tab ?
e.g :
<record model="ir.ui.view" id="partner_sessions_form_view">
<field name="name">partner.sessions.name</field>
<field name="model">res.partner</field>
<field name="type">form</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<xpath expr="/form/notebook/page[#string='Notes']" position="after">
<page string="Sessions">
<field name="session_ids" nolabel="1" colspan="4"/>
</page>
</xpath>
</field>
</record>
This adds a page, but how would I add a field in the page Sessions ?

If you want to add a field to page 'Sessions' that you have created above, then you should provide
<record model="ir.ui.view" id="partner_sessions_form_view_again_inherited">
<field name="name">partner.sessions.name.inherited</field>
<field name="model">res.partner</field>
<field name="type">form</field>
<field name="inherit_id" ref="partner_sessions_form_view" />
<field name="arch" type="xml">
<xpath expr="//field[#name='session_ids']" position="before">
<field name="new_field1" nolabel="1" colspan="4"/>
</xpath>
</field>
</record>
if you want to add add a field to the session_ids tree or form view, then
<xpath expr="//field[#name='session_ids']/tree/field[#name='already_existing_field']" position="after">
<field name="new_field1" />
</xpath>

You need to find that field in side you page Session
like this (but that page has to be their in view):
<xpath expr="//page[#string='Sessions']/field[#name='some_field'] position="after">
<field name="session_ids" nolabel="1" colspan="4"/>
</xpath>
Thanks

This is covered pretty well in the documentation. The simplest case is to just add your new field after an existing one. In this example, you're placing the relation_ids field after the lang field.
<record model="ir.ui.view" id="view_partner_form4">
<field name="name">res.partner.form.inherit4</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="lang" position="after">
<field name="relation_ids"/>
</field>
</field>
</record>
If that field appears in more than one place, you'll have to use an XPath element. For example, if the lang field appears in a subview's tree view and its form view.

Related

Add field in wizard view inside inherited module

I have a wizard with a m2m field that displays library.book tree.
In parent module view:
<field name='model'>library.return.wizard</field>
<field name='arch' type='xml'>
<form string="Return books">
<sheet>
<group>
<field name='book_ids'>
<tree>
<field name="name"/>
<field name="state"/>
<!--How to add date_return field here?-->
<!--<field name="date_return"/>-->
</tree>
</field>
</group>
</sheet>
</form>
</field>
Another module model inherits library.book and adds a field date_return.
class LibraryBook(models.Model):
_inherit = 'library.book'
date_return = fields.Date('Date to return')
I tried to add a date_return field to the wizard view like this, in the child module view:
<field name='model'>library.return.wizard</field>
<field name='arch' type='xml'>
<field name='book_ids' position="inside">
<tree>
<xpath expr="field[#name='state']" position="after">
<field name="date_return" />
</xpath>
</tree>
</field>
</field>
It doesn't work. What is the right way to address it in the view?
Again, I have a module that inherits m2m field model, and add a field there, but I can't get the field.
UPDATE 22 Jun:
inherit_id definitely helped. Parent record is <record id='library_return_wizard_form' model='ir.ui.view'> so my child view uses <field name="inherit_id" ref="my_library.library_return_wizard_form"/>.
When i only use an element locator, the only column rendered in the tree widget is state.
<record id='library_return_wizard_form_add' model='ir.ui.view'>
<field name='name'>library.return.wizard.form.view.add</field>
<field name='model'>library.return.wizard</field>
<field name="inherit_id" ref="my_library.library_return_wizard_form"/>
<field name='arch' type='xml'>
<field name='book_ids' position="inside">
<tree>
<field name="state" position="after">
<field name="date_return"/>
</field>
</tree>
</field>
</field>
</record>
If I try to go with xpath, there is a js frontend error.
<record id='library_return_wizard_form_add' model='ir.ui.view'>
<field name='name'>library.return.wizard.form.view.add</field>
<field name='model'>library.return.wizard</field>
<field name="inherit_id" ref="my_library.library_return_wizard_form"/>
<field name='arch' type='xml'>
<field name='book_ids' position="inside">
<tree>
<xpath expr="//field[#name='book_ids']/tree/field[#name='state']" position="after">
<field name="date_return" />
</xpath>
</tree>
</field>
</field>
</record>
Maybe, just maybe the field is not being found with this xpath, but I doubt it.
Full error:
TypeError: Cannot read property 'type' of undefined
at Class._renderBodyCell (http://localhost:8069/web/static/src/js/views/list/list_renderer.js:317:55)
at http://localhost:8069/web/static/src/js/views/list/list_renderer.js:593:25
at Function._.map._.collect (http://localhost:8069/web/static/lib/underscore/underscore.js:164:24)
at Class._renderRow [as _super] (http://localhost:8069/web/static/src/js/views/list/list_renderer.js:592:24)
at Class._renderRow (http://localhost:8069/web/static/src/js/views/list/list_editable_renderer.js:566:32)
at Class._renderRow (http://localhost:8069/web/static/src/js/core/class.js:123:38)
at Function._.map._.collect (http://localhost:8069/web/static/lib/underscore/underscore.js:164:24)
at Class._renderRows (http://localhost:8069/web/static/src/js/views/list/list_renderer.js:614:18)
at Class._renderRows (http://localhost:8069/web/static/src/js/views/list/list_editable_renderer.js:588:26)
at Class._renderRows (http://localhost:8069/web/static/src/js/core/class.js:123:38)
In list_renderer.js file xpath node has all it's attributes except expr undefined.
FINAL EDIT:
I should have removed also these few elements which I didn't notice after staring at this one file for too long. Locator or xpath should have been put right into arch.
<field name='book_ids' position="inside">
<tree>
You should use the inherit_id field to specify the parent view of the current view, it is unset by default.
You are trying to locate the field named book_ids and add a tree to it which has an XPath expression to locate the state field. The correct way is to locate the state field and use the position attribute to specify how the matched node should be altered.
<record id="view_library_return_wizard_form" model="ir.ui.view">
<field name="name">library.return.wizard.form.inherit</field>
<field name="model">library.return.wizard</field>
<field name="inherit_id" ref="MODULE_NAME.FORM_VIEW_ID"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='book_ids']/tree/field[#name='state']" position="after">
<field name="date_return"/>
</xpath>
</field>
</record>
When using an element locator, Odoo will try to find the first element matching. You can use the field locator to simplify the syntax (if there is only one state field or you need to locate the first one).
<record id="view_library_return_wizard_form" model="ir.ui.view">
<field name="name">library.return.wizard.form.inherit</field>
<field name="model">library.return.wizard</field>
<field name="inherit_id" ref="MODULE_NAME.FORM_VIEW_ID"/>
<field name="arch" type="xml">
<field name="state" position="after">
<field name="date_return"/>
</field>
</field>
</record>
EDIT::
Inheritance specs are comprised of an element locator, to match the inherited element in the parent view, and children element that will be used to modify the inherited element.
You have just to include one of the following expressions in the view arch
xpath:
<xpath expr="//field[#name='book_ids']/tree/field[#name='state']" position="after">
<field name="date_return"/>
</xpath>
field:
<field name="state" position="after">
<field name="date_return"/>
</field>

Odoo How to add a editable tree view inside a new page?

I want to display the fields as editable in tree view inside a new tab in the sale.order? My code does not make any errors, but shows the fields in form view rather than tree view. How to correct it?
.xml
<record model="ir.ui.view" id="contract_custom">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="type">tree</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='Other Information']" position="after">
<page string="Salesman Commission">
<tree editable="bottom">
<group>
<field name="user_select"/>
<field name="sales_value" on_change="on_change_commission(sales_value,sale_percent)"/>
<field name="sale_percent" on_change="on_change_commission(sales_value,sale_percent)"/>
<field name="sale_commission"/>
</group>
</tree>
</page>
</xpath>
</field>
</record>
Here is a possible solution
<record model="ir.ui.view" id="contract_custom">
<field name="name">sale.order.custom.form.inherited</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='Other Information']" position="after">
<page string="Salesman Commission">
<field name="field_name"> <!-- field you want to show as tree -->
<tree editable="bottom">
<field name="user_select"/>
<field name="sales_value" on_change="on_change_commission(sales_value,sale_percent)"/>
<field name="sale_percent" on_change="on_change_commission(sales_value,sale_percent)"/>
<field name="sale_commission"/>
</tree>
</field>
</page>
</xpath>
</field>
</record>

Odoo not displayed field

I get a problem with displaying custom field in sale order view.
sale_order.py
...
'typeship' : fields.many2one('vips_shop.delivery', string="Type delivery", readonly=False),
'usersess' : fields.many2one('vips_vc.session', string="Session customer", readonly=False),
...
sale_order.xml
<record model="ir.ui.view" id="sale_order_usersess_form_view">
<field name="name">order.usersess</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="client_order_ref" position="after">
<field name="usersess" string="User Session ID"/>
</field>
</field>
</record>
<record model="ir.ui.view" id="sale_order_typeship_form_view">
<field name="name">order.typeship</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="client_order_ref" position="after">
<field name="typeship" string="Type shipping"/>
</field>
</field>
</record>
As result i received this: field usersess displayed fine, typeship - not displayed.
Why is it happening? All fields for usersess and typeship is equal.
Thanks for Hardik Patadia
why are you having two separate views for showing two fields? Why are
you not showing both of them in single inherited view? I think view
priority may help you
When I'm showing in single view all displayed fine:
<record model="ir.ui.view" id="sale_order_usersess_form_view">
<field name="name">order.usersess</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<field name="client_order_ref" position="after">
<field name="usersess" string="User Session ID"/>
<field name="typeship" string="Type shipping"/>
</field>
</field>
</record>

how can I merge two tree views?

I have two tree views, how can I merge it in one but still have the same number of lines because my problem is that I wanna separate a line into two as i didn't find a solution I created the two tree views, so if there is another way to separate the line without creating the two views is better.
<record model="ir.ui.view" id="medical_lab_cash_tree_id">
<field name="name">cash</field>
<field name="model">medical.lab.patient</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree>
<field name="patient_id"/>
<field name="Amount_in_date"/>
<field name="Amount_In"/>
<field name="type_In"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="medical_lab_cash_tree_id2">
<field name="name">cash2</field>
<field name="model">medical.lab.patient</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree>
<field name="patient_id"/>
<field name="Amount_in_date1"/>
<field name="Amount_In1"/>
<field name="type_In1"/>
</tree>
</field>
</record>
We can inherit original view and add as needed fields in tree view.
For example:
<record id="medical_lab_cash_tree_id_extend" model="ir.ui.view">
<field name="name">medical.lab.cash.tree</field>
<field name="model">medical.lab.patient</field>
<field name="inherit_id" ref="module_name.medical_lab_cash_tree_id"/>
<field name="arch" type="xml">
<field name="type_In" position="after">
<field name="Amount_in_date1"/>
<field name="Amount_In1"/>
<field name="type_In1"/>
</field>
</field>
</record>
EDIT: You can replace your whole tree with following style.
<record id="medical_lab_cash_tree_id_extend" model="ir.ui.view">
<field name="name">medical.lab.cash.tree</field>
<field name="model">medical.lab.patient</field>
<field name="inherit_id" ref="module_name.medical_lab_cash_tree_id"/>
<field name="arch" type="xml">
<xpath expr="/tree" position="replace">
<tree>
<!-- Place your new field list -->
</tree>
</xpath>
</field>
</record>

readonly in invoice lines based on a value in line

I have a field 'uneditable' in 'account.invoice.line'.
I want to disable editing the line record if uneditable is true but creating enw line should be allowed.
my view code is as follows
<record model="ir.ui.view" id="invoice_supplier_form_ext">
<field name="name">account.invoice.supplier.form</field>
<field name="model">account.invoice</field>
<field name='inherit_id' ref='account.invoice_supplier_form'/>
<field name="type">form</field>
<field name="arch" type="xml">
<data>
<field name="product_id" position="before">
<field name="uneditable" invisible="1"/>
</field>
<field name="quantity" position="attributes">
<attribute name="attrs">{'readonly': [('uneditable','=', True)]}</attribute>
</field>
</data>
</field>
</record>
Please suggest me what i am doing wrong. I am getting error with this code in view and does not make the line readonly.
Actually your view is wrong. You have defined the fields 'quantity' and 'uneditable' in account.invoice.line and you are adding then in the invoice view. Please check the invoice_supplier_form view and add the fields correctly in the view.
<record model="ir.ui.view" id="invoice_line_form_ext">
<field name="name">account.invoice.line.form</field>
<field name="model">account.invoice.line</field>
<field name='inherit_id' ref='account.view_invoice_line_form'/>
<field name="type">form</field>
<field name="arch" type="xml">
<data>
<field name="product_id" position="before">
<field name="uneditable" invisible="1"/>
</field>
<field name="quantity" position="attributes">
<attribute name="attrs">{'readonly': [('uneditable','=', True)]}</attribute>
</field>
</data>
</field>
</record>