Odoo 10 - Extend view to include related model field - odoo

I am trying to extend the view which displays packages in Odoo 10 so it display also the product_id:
<record id="stock_view_picking_form_enhanced" model="ir.ui.view">
<field name="name">stock_view_picking_form_enhanced</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="priority">20</field>
<field name="arch" type="xml">
<xpath expr="//field[#name='pack_operation_product_ids']/tree/field[#name='package_id']" position="after">
<field name="package_id.product_id"/>
</xpath>
</field>
</record>
So basically I am looking to display related field product_id from pack_operation_product_id.
Which is the right approach to implement this?

First you must include the related field in your inherited python class.
class StockPicking(models.Model):
_inherit = "stock.picking"
product_id = fields.Many2one('product.product', related='package_id.product_id', string='Product', store=True)
Then in XML you can write like the following:
<record id="stock_view_picking_form_enhanced" model="ir.ui.view">
<field name="name">stock_view_picking_form_enhanced</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form"/>
<field name="priority">20</field>
<field name="arch" type="xml">
<xpath expr="//field[#name='pack_operation_product_ids']/tree/field[#name='package_id']" position="after">
<field name="product_id"/>
</xpath>
</field>
</record>

Related

How to hide discount field in Invoice form? Odoo

I have try to hide discount field in invoice form by xpath. However it doesn't work.
The following is my code :
<odoo>
<data>
<record model="ir.ui.view" id="hidden_discount">
<field name="name">account.invoice.hidden.discount</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='discount']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
Do you have any solution or suggestion?
There is no direct form view design for One2many field. So we need to upgrade account.invoice.line form view directly.
Try with following code.
<record model="ir.ui.view" id="hidden_discount">
<field name="name">account.invoice.line.hidden.discount</field>
<field name="model">account.invoice.line</field>
<field name="inherit_id" ref="account.view_invoice_line_form"/>
<field name="arch" type="xml">
<field name="discount" position="attributes">
<attribute name="invisible">1</attribute>
</field>
</field>
</record>

Odoo. How to create a view to extended model

I am very new in odoo and i really need your help.
I've extended res.partner :
class extendedPartner(models.Model):
_name = 'extended.partner'
_inherit = 'res.partner'
auto = fields.One2Many('partner.car', 'auto_name', 'Car', required=False)
class partnerCar(models.Model):
_name = 'partner.car'
auto_model = fields.Char('Model auto', size=20, required=True)
release = fields.Integer('Year of release', required=True)
auto_name = fields.Many2One('extended.partner', 'Car Name', required=True)
But I don't know how to write xml so that I could see all partner's cars and information about them
<record model="ir.ui.view" id="view_partner_form">
<field name="name">res.partner.form.inherit</field>
<field name="model">extended.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<notebook position="inside">
<page string="Cars">
<!-- what should I write here? -->
</page>
</notebook>
</field>
</record>
Could you please help me? Thank you in advance.
UPD:
Is it right solution?
<record model="ir.ui.view" id="view_partner_form">
<field name="name">res.partner.form.inherit</field>
<field name="model">extended.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<field name="auto">
<tree>
<field name="auto_name"/>
<field name="auto_model"/>
<field name="release"/>
</tree>
</field>
</field>
</record>
You are almost there, since you inherit from another view and you injecting your view into the view you inherit from, you need to give your new view a "hook" in the parent view which it can use to attach its contents. So you use an xpath expression, and then you insert your field.
When you insert a relational field you can create so called embedded views. Here you have defined a tree view for your field. That means whenever your field will be rendered as a tree that is the tree that will be used, in your case the form.
You could also create a <form> after <tree> to be shown when clicked.
<record model="ir.ui.view" id="view_partner_form">
<field name="name">res.partner.form.inherit</field>
<field name="model">extended.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="insert_x_path_xpression here" position="after, before etc"
<field name="auto">
<tree>
<field name="auto_name"/>
<field name="auto_model"/>
<field name="release"/>
</tree>
</field>
</xpath>
</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>

Openerp 6.1 put product category in sale order line

I want to show the product category in the sale.order.line.tree view of a sales order with the following code which I wrote. It shows the category button under the group by button but on clicking it, I get the following error and I have don't know how to solve the bug:assert groupby_def and groupby_def._classic_write, "Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True"
AssertionError: Fields in 'groupby' must be regular database-persisted fields (no function or related fields), or function fields with store=True
Here is my code:
from osv import fields, osv<code>
class sales_order_line_category(osv.osv):
_name='sale.order.line'
_inherit='sale.order.line'
_columns={'categ_id': fields.related('product_id', 'categ_id', type='many2one', relation='product.categ_id'),
}
sales_order_line_category()
My view:
`<?xml version="1.0" encoding="utf-8"?>
<record id="view_sale_orderlinecategory" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit</field>
<field name="model">sale.order.line</field>
<field name="type">tree</field>
<field name="inherit_id" ref="sale.view_order_line_tree"/>
<field name="arch" type="xml">
<field name="name" position="after">
<field name="categ_id" string="Category"/>
</field>
</field>
</record>
<record id="view_sale_orderlinecategory2" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit2</field>
<field name="model">sale.order.line</field>
<field name="type">search</field>
<field name="inherit_id" ref="sale.view_sales_order_uninvoiced_line_filter"/>
<field name="arch" type="xml">
<group expand="0" string="Group By..." >
<filter string="Category of Product" icon="terp-stock_symbol-selection" name="Category" context="{'group_by':'categ_id'}"/>
</group>
</field>
</record>
<record id="view_sale_orderlinecategory3" model="ir.ui.view">
<field name="name">sale.order.line.categoryinherit3</field>
<field name="model">sale.order,line</field>
<field name="type">search</field>
<field name="inherit_id" ref="sale.view_sales_order_uninvoiced_line_filter"/>
<field name="arch" type="xml">
<field name="name" position="before">
<field name="categ_id" string="Category"/>
</field>
</field>
</record>
</data>
`
WOuld you please try with following code replace?
'categ_id': fields.related('product_id', 'categ_id', type='many2one', relation='product.category', store=True),
Check that your field categ_id must be appear in tree view of sale.order.line where you try to do group by categ_id
Hope this help

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

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.