Odoo 10 - QWeb hide a view element if a field is False - odoo

I have following QWeb element:
<record id="extended_res_partner" model="ir.ui.view">
<field name="name">Extended View</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="model">res.partner</field>
<field name="arch" type="xml">
<notebook position="inside">
<page string="Foo" name="foo" attrs="{'invisible': [('is_customer', '=', False),]}">
<field name="is_customer" invisible="1"/>
<span>Foo2</span>
</page>
</notebook>
</field>
</record>
But it does not work. I get:
Field `is_customer` does not exist
If I remove attrs=... it works fine.

Even that you didn't provide the error message but this will work only if the the form view if for res.partner but i'm assuming that the form is for another model that have a many2one relation with res.partner in this case you need to create a related field in the model.
partner_id = ......
is_customer = fields.Boolean(related='partner_id.is_customer', readonly=True)
Then you need to add this field to the form view because attrs is client side feature it need the value in form in order to work.
<page string="foo" name="foo" attrs="{'invisible': [('is_customer', '=', False),]}">
<field name="is_customer" invisible="1"/>
<span>Foo2</span>
</page>
Note: if the form view is for res.partner just add the field to the form view because as I said is a client side operation it will not call the server to know what is the value of that field you need to pass it.

Related

Odoo - How add a field to view from another wizard model?

I want the use a field from wizard to my form view.
Let me explain with code:
class CancelAppointmentWizard(models.Model):
_name = "cancel.appointment.wizard"
_description = "Cancel Appointment Wizard"
reason = fields.Text(string="Reason")
and i want the show this "reason" field inside of some form views.
<record id="view_hospital_appointment_form" model="ir.ui.view">
<field name="name">hospital.appointment.form</field>
<field name="model">hospital.appointment</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="reason"/>
</group>
</sheet>
</form>
</field>
</record>
but of course this give me error like
hospital.appointment don't have a field like reason.
How can show this field ?
I tried to make a dummy code, i hope i was able to explain my problem.
I assume the wizard is called from the view_hospital_appointment_form. And in the wizard view let say there is a button tag with name attribute, which is this attribute associated to python method in your wizard class.
Wizard view
<record id="view_cancel_appointment_wizard_form" model="ir.ui.view">
<field name="name">cancel.appointment.wizard.form</field>
<field name="model">cancel.appointment.wizard</field>
<field name="arch" type="xml">
<form>
…
…
<footer>
<button name="action_ok" string="Ok" type="object"/>
</footer>
</form>
</field>
</record>
Python method
def action_ok(self):
ids = self.env.context.get('active_ids')
hospital_appointment = self.env['hospital.appointment'].browse(ids)
hospital_appointment.reason = self.reason
return {'type': 'ir.actions.act_window_close'}

How to edit invoice template on Odoo15

I created custom fields in Odoo v15.
PROBLEM
Now I would like to add them to invoice template.
How I could do it?
You need to inherit the form view (account.invoice.form) and add your custom fields to the view. Or if you want to test the view you can enable debug mode and click "Edit FormView" at the top right of your form view to (temporarily) edit the form view directly.
See my example below:
<record id="account_invoice_form_custom" model="ir.ui.view">
<field name="name">account.invoice.form.custom</field>
<field name="model">account.invoice</field>
<field name="inherit_id" ref="account.invoice_form" />
<field name="arch" type="xml">
<field name="partner_id" position="after">
<field name="custom_field"/>
</field>
</field>
</record>

Odoo: Display a field in tree view depending on value of another field in the model

I have a tree view in which I want to display a column depending on the value of another field. To be specific, in the Inventory app, I want to add a column in the tree view when the picking type is 'Internal Transfers'. I do not want to show the same column in any other picking type.
Please note, I am customizing this in Odoo Enterprise Edition.
I did attrs="{'invisible': [('x_picking_type_name','=', 'Internal Transfers')]}", where x_picking_type_name is a custom field in the model. I am able to hide values in the records but the column remains in other picking types.
I suppose, there is a way around with context but I could not make it work. I will appreciate any help on this.
The XML I am using. I am trying it in original view without inheriting.
<?xml version="1.0"?>
<tree decoration-info="state == 'draft'" decoration-muted="state == 'cancel'" decoration-danger="state not in ('cancel', 'done') and min_date < current_date" string="Picking list">
<field name="name"/>
<field name="location_dest_id"/>
<field name="partner_id"/>
<field name="date" invisible="1"/>
<field name="min_date"/>
<field name="origin"/>
<field name="x_picking_type_name"/>
<field name="check_todo" attrs="{'invisible': [('x_picking_type_name','!=', 'Internal Transfers')]}"/>
<field name="group_id" invisible="1"/>
<field name="backorder_id"/>
<field name="state"/>
<field name="priority" invisible="1"/>
<field name="picking_type_id" invisible="1"/>
<field name="product_id"/>
</tree>
According to this post
https://www.odoo.com/es_ES/forum/ayuda-1/how-to-add-a-field-conditional-to-tree-view-of-customer-14707#answer_198626
`attrs="{'invisible':[('customer','!=',True)]}"`

How can i set my own form view to enter customer data into res.partner model in odoo

How can I set my own form view to enter customer data into res.partner model without using base.view_partner_form form view of res_partner model?
First off, you will need the custom form view, of course.
And after that it depends, what you want to do.
Change the form view everywhere: Give your form view a lesser priority than "base.view_partner_form":
<record id="my_partner_form" model="ir.ui.view">
<field name="name">...</field>
<!-- other fields -->
<field name="priority">1</field>
<!-- arch -->
</record>
Or if you want to get a new menuitem for the users, just create one. Then create a ir.actions.act_window with your new form view as default view and link it to your new menuitem.
You can create new partner form and set priority.
By default system will load minimum priority form in odoo.
View default priority is 16 & if you give your view priority is 20 then default from view will be load.
Ex:
<record id="view_partner_title_form" model="ir.ui.view">
<field name="name">res.partner.title.form</field>
<field name="model">res.partner.title</field>
<field name="priority">20</field>
<field name="arch" type="xml">
<form string="Partner Titles">
<group col="4">
<field name="name"/>
<field name="shortcut"/>
</group>
</form>
</field>
</record>
Now your new form view priority is 20, so system will load default form view because default form view priority is 16.
You can create ir.actions.act_window.view
Based on that system will load any sequence form/tree view based on your requirement.
<record id="action_portal_partner_form" model="ir.actions.act_window">
<field name="name">Customers</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">res.partner</field>
<field name="domain">[('customer','=',True)]</field>
<field name="view_type">form</field>
<field name="view_mode">kanban,tree,form</field>
<field name="context">{"search_default_customer":1}</field>
<field name="search_view_id" ref="base.view_res_partner_filter"/>
</record>
<record id="action_portal_form_view2" model="ir.actions.act_window.view">
<field eval="23" name="sequence"/>
<field name="view_mode">form</field>
<field name="view_id" ref="view_partner_title_form"/>
<field name="act_window_id" ref="action_portal_partner_form"/>
</record>
View_mode : tree/form/..
View Id : your view id
Action : You must write correct action id

How to hide fields based on condition in odoo?

I have created few custom fields for products.Products appear in sales,purchase,warehouse and manufacturing module.I want to make my custom fields appear only in manufacturing module and hide everywhere else.So how to put condition on invisible attribute.I tried like this and got error as Unknown field _name in domain
attrs="{'invisible': [('_name', '!=', 'mrp.bom')]}"
Python file,
from openerp import fields,models, api
class product_template(models.Model):
_inherit = "product.template"
rim_weight = fields.Float(string='Rim Weight(KG)', readonly=True, compute='_compute_one_rim_weight')
width = fields.Float(string='Width(cm)',default='50')
length = fields.Float(string='Length(cm)',default='63')
gsm = fields.Float(string='Gram per square meter(gsm)',default='230')
Xml file,
<record id="product_template_form_view_dis_inherit" model="ir.ui.view">
<field name="name">product.template.common.form.dis.inherit</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view"/>
<field name="arch" type="xml">
<xpath expr="//page[#string='Accounting']" position='after'>
<page string='Cover Page Paper'>
<group>
<field name="width"/>
<field name="length"/>
<field name="gsm"/>
<field name="rim_weight"/>
</group>
</page>
</xpath>
</field>
</record>
There are many ways to do this, however I suggest following options to you.
Modify an existing action and set context inside that and based on that context just write condition in view. (Remember here you need to override an action, and if you want to create another then you need to redefine menu to assign new action to that menu).
Create new view and set this view reference in an action of that model in which you want to show these fields. In new view you need to visible those fields, no need to extend product template existing view.
However the 1st solution is easy to achieve and 2nd one is lengthy.
Example of 1st solution:
<record id="action_name" model="ir.actions.act_window">
<field name="name">Name</field>
<field name="res_model">product.template</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="context" eval="{'is_manufacturing_model':True}" />
</record>
And then in view just write like this
<page string='Cover Page Paper'>
<group invisible="context.get('is_manufacturing_model',False)">
<field name="width"/>
<field name="length"/>
<field name="gsm"/>
<field name="rim_weight"/>
<group>
</page>