Odoo - Adding domain filters to tree views in tabs - odoo

I am new to Odoo, but I have been tasked to create a tree view in the partners form. The tree view has to display a list of an individual's "active" contracts. Unfortunately, setting the domain logic on the tree view does not seem to work. What is the pattern for doing this in Odoo?
<record model="ir.ui.view" id="view_partner_form_inherit">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<notebook position="inside">
<page string="Contracts">
<field name="contract_ids">
<tree domain="[('state', '=', 'active')]">
<field name="state" invisible="1" />
<field name="name" />
<field name="arrears" />
<field name="total_amount_paid" />
</tree>
</field>
</page>
</notebook>
</field>
</record>

Atleast for One2many fields it should be possible by defining a field twice with different names and ofcourse one of them with the domain, for example:
contract_ids = fields.One2many(
comodel_name="contract.model", inverse_name="inverse_id",
string="All Contracts")
active_contract_ids = fields.One2many(
comodel_name="contract.model", inverse_name="inverse_id",
string="Active Contracts", domain="[('state', '=', 'active')]")

Related

Hide field from tree view base on the condition, Odoo

I want to hide field from tree view depends on the condition. I tried some techniques, but didn't work for me. Currently I can hide just value not all column. I want to hide whole column.
This is my tree view. And this code just hide the value not column.
<record id="view_tai_approval_tree" model="ir.ui.view">
<field name="name">tai.approval.tree</field>
<field name="model">tai.approval</field>
<field name="arch" type="xml">
<tree create="false">
<field name="tai_reference"/>
<field name="tai_project_name"/>
<field name="responsible_name"/>
<field name="tai_purchase_date"/>
<field name="tai_supplier_name"/>
<field name="state" class="bg-danger" attrs="{'invisible': [('state', '!=', 'unapproved')]}"/>
<field name="state" class="bg-success" attrs="{'invisible': [('state', '!=', 'approved')]}"/>
<field name="state" class="bg-warning" attrs="{'invisible': [('state', '!=', 'pending')]}"/>
</tree>
</field>
</record>
Actually, as you can see in the code, I just want to change the colors depends on the states. So is there any simple solution for this?
Odoo allows you to change the behavior of the text based on the attributes of the corresponding record.
This documentation is for tree views, but what they describe also works for fields.
<record id="view_tai_approval_tree" model="ir.ui.view">
<field name="name">tai.approval.tree</field>
<field name="model">tai.approval</field>
<field name="arch" type="xml">
<tree create="false">
<field name="tai_reference"/>
<field name="tai_project_name"/>
<field name="responsible_name"/>
<field name="tai_purchase_date"/>
<field name="tai_supplier_name"/>
<field name="state"
decoration-danger=="state != 'unapproved'"
decoration-success="state != approved'"
decoration-warning="state != 'pending'"/>
</tree>
</field>
</record>

2 fields in tree view

In the product category, I added a product_ids field and I want to display it with a tree view showing name and default_code of products. For some reason I get the error "Field default_code does not exist"
<record id="view_product_category_qty_discount" model="ir.ui.view">
<field name="name">product.category.inherit.qty.discount.Config Hetlita</field>
<field name="model">product.category</field>
<field name="type">form</field>
<field name="inherit_id" ref="product.product_category_form_view" />
<field name="arch" type="xml">
<form position="inside">
<group col="2" colspan="2">
<separator string="Quantity for discount" colspan="2"/>
<field name="qty_for_discount" />
</group>
<group>
<field name="product_ids" widget="many2many_tags"/>
<tree>
<field name="name"/>
<field name="default_code"/>
</tree>
</group>
</form>
</field>
</record>
class ProductCategory(models.Model):
_inherit = 'product.category'
qty_for_discount = fields.Float(string='Qty For Discount')
product_ids = fields.Many2many(
'product.template', string='Products')
That's because there is no default_code on model product.template but instead on its variants with model product.product. I would change the field on product.category to a One2Many on product.product:
product_ids = fields.One2many(
comodel_name='product.product',
inverse_name='categ_id',
string='Products')
And there is a mistake in your xml:
<group>
<field name="product_ids">
<tree>
<field name="name"/>
<field name="default_code"/>
</tree>
</field>
</group>

Unknown field 'state' in domain when editing attribute

Using Odoo 10 (fetched from GitHub commit 7413b26, branch 10.0), installing a module which I'm porting over from Odoo 8.
This module forces the purchase.order.line form to show up upon clicking a line in purchase.order by removing the editable attribute from the tree, but when saving changes done in that form Odoo raises:
Error: Unknown field state in domain [["state","in",["purchase","to approve","done","cancel"]]]
purchase_order_error.xml:
<record id="purchase_order_line_tree" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<field name="model">purchase.order</field>
<field name="priority" eval="33"/>
<field name="type">form</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook/page/field[#name='order_line']/tree" position="attributes">
<attribute name="editable"/>
</xpath>
</field>
</record>
The __manifest__.py is:
{'name': "purchase_order_error",'depends': ['base', 'product', 'purchase'],'data': ['purchase_order_error.xml',],'installable':True}
and __init__.py is just the usual from . import purchase_order_error.
Here are some more observations:
The field state can have the following Selection values: [draft], [sent], [to approve], [purchase], [done], [cancel]; [draft] and [sent] do not appear in the error.
An Odoo user reported same problem on the Odoo GitHub bug tracker, without any solution as the time of writing.
Is there a workaround ?
You should add
<record id="purchase_order_line_tree" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<field name="model">purchase.order</field>
<field name="priority" eval="33"/>
<field name="type">form</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook/page/field[#name='order_line']/tree" position="attributes">
<attribute name="editable">top</attribute>
</xpath>
</field>
</record>
In your case it's going to open form view to allow you to create
records, because you have not given the value of editable attribute.
So in form view of purchase order line there might be no field defined "state". According to rule if fields are used anywhere in attrs, domains then it must be defined on view, it doesn't matter you can define it invisible.
So just add this field in Purchase order line form view or follow the first solution for editable="top"
<field name="state" invisible="1" />
The problem is when saving the record from the form view
the state field is not defined. you don't get this error
in the tree view because the field is there:
<page string="Products">
<field name="order_line" attrs="{'readonly': [('state', 'in', ('done', 'cancel'))]}">
<tree string="Purchase Order Lines" editable="bottom">
<field name="currency_id" invisible="1"/>
<field name="state" invisible="1"/>
.....
....
..
so you need to add it to the embedded form too:
<record id="xxx_purchase_order_form_list_details" model="ir.ui.view">
<field name="name">xxx_purchase_order_form_list_details</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[#name='order_line']/tree" position="attributes">
<attribute name="editable"/>
</xpath>
<xpath expr="//field[#name='order_line']//form//field[#name='product_id']" position="before">
<field name="state" invisible="1"/>
</xpath>
</field>
</record>

OpenERP version 6, My Timesheet form crashed after inherited

In OpenERP version 6, in my custom module, I changed the Timesheet form view by inheriting as my custom view below:
<record id="oit_hr_timesheet_sheet_form" model="ir.ui.view">
<field name="name">oit.hr.timesheet.sheet.form</field>
<field name="model">hr_timesheet_sheet.sheet</field>
<field name="inherit_id" ref="hr_timesheet_sheet.hr_timesheet_sheet_form" />
<field name="type">form</field>
<field name="arch" type="xml">
<data>
<button name="sign_in" position="replace"/>
<button name="sign_out" position="replace"/>
<field name="state_attendance" position="after">
<field name="day_attendance"/>
</field>
<field name="total_attendance_day" position="replace"/>
<field name="total_difference_day" position="replace"/>
<field name="total_timesheet_day" position="replace"/>
<field name="timesheet_ids" position="after">
<field name="day_difference"/>
<field name="day_timesheet"/>
</field>
<page string="By Day" position="replace" />
<notebook position="inside">
<page string="Time balance">
<group colspan="4" col="2" width="600">
<field name="time_account_balance"/>
<field name="working_hour"/>
<field name="attendance_hour"/>
<separator string="Timesheet numbers until the viewed working period." colspan="4" />
<field name="total_time_account_balance"/>
<field name="total_working_hour"/>
<field name="total_attendance_hour"/>
</group>
</page>
</notebook>
</data>
</field>
</record>
Then the Human Resources / Time Tracking / My Timesheet form totaly crash. But when I go to Administration / Customization / User Interface / Menu Items, and I choose "My Timesheet" menu item to fix its action from "ir.actions.server" to "ir.actions.act_window" and select object "hr_timesheet_sheet.sheet", then choose my view "oit.hr.timesheet.sheet.form" for object's form view, choose my view "oit.hr.timesheet.sheet.tree.simplified" for object's tree view.
Then I have "My Timesheet" form shown correctly.
Why is that? I think there is somethings in my view code not work with "My Timesheet" menu and "ir.actions.server" defined in file hr_timesheet_sheet/hr_timesheet_sheet_data.xml, Here I paste the openerp6 code in hr_timesheet_sheet_data.xml:
<record id="ir_actions_server_timsheet_sheet" model="ir.actions.server">
<field eval="5" name="sequence"/>
<field eval=""""code"""" name="state"/>
<field eval=""""ir.actions.server"""" name="type"/>
<field name="model_id" ref="model_hr_timesheet_current_open"/>
<field eval="[(6,0,[])]" name="child_ids"/>
<field eval=""""action = self.pool.get('hr.timesheet.current.open').open_timesheet(cr, uid, object.id, context)"""" name="code"/>
<field eval=""""True"""" name="condition"/>
<field eval=""""My Timesheet"""" name="name"/>
</record>
<record id="menu_act_hr_timesheet_sheet_form_my_current" model="ir.ui.menu">
<field name="name">My Timesheet</field>
<field eval="1" name="sequence"/>
<field name="parent_id" ref="hr_attendance.menu_hr_time_tracking"/>
<field name="icon">STOCK_JUSTIFY_FILL</field>
<field name="action" ref="ir_actions_server_timsheet_sheet"/>
</record>
Kindly please help me to have correct view code or a solution so that I will not need to change information for menu "My Timesheet" after I upgrade my custom module! thanks a lot!

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.