Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

How to add a field conditional to tree view of customer?

By
Anabela Damas
on 5/3/13, 10:57 AM 9,552 views

Hi,

I've added a supplier number and a customer number, and now I need to add this number in the tree view but I just want that the "Supplier number " appears if there is a partner is a supplier or in the view. I'll try to explain with images.

In this case when a partner is both(supplier and a costumer), it's ok to look like this: both

In this case I didn't want to show the Supplier Number: image description

And in this case I didn't want to show the Client Number: image description

I'm using this code:

<record id="view_partner_tree_inherit" model="ir.ui.view">
    <field name="name">res.partner.tree.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_tree" />
    <field name="arch" type="xml">
    <xpath expr="/tree/field[@name='name']" position="after">
        <field name="supplier" invisible="1"/>
        <field name="customer" invisible="1"/>
        <field name="n_client" attrs="{'invisible':[('customer','!=',True)]}"/>
        <field name="n_supplier" attrs="{'invisible':[('supplier','!=',True)]}"/>
        </xpath>
    </field>
</record>

Someone know how to hide the columns depending on if it is a Suppliers tree view or a Customers tree view ?

Thanks

1
vim24
On 5/16/13, 8:44 PM

Having just scoured the internet looking for an answer to a similar problem myself I can tell you how I finally got something similar working:

Instinct told me to try the

attrs="{'invisible':[('customer','!=',True)]}"

approach too. But, I believe attrs don't work to stop the column name itself from appearing in a tree view (works only on the values of each row).

Instead, you need to overwrite the fields_view_get method from within your res_partner python code, which will allow you to change the viability of each column by checking the context. In my case I was looking at Supplier vs Customer invoices, so I checked for the context 'type' being 'out_invoice'/'in_invoice'. In your case I believe customers vs suppliers are given a context value, something like "default_customer" and "default_supplier".

Anyway, in the end you want to remove the attrs element from you xml view and then add something like this to the python code:

  from lxml import etree

  def fields_view_get(self, cr, uid, view_id=None, view_type=False, context=None, toolbar=False, submenu=False):

    if context is None:
        context = {}

    res = super(res_partner,self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)

    doc = etree.XML(res['arch'])

    if view_type == 'tree':
        if context.get('default_customer', '0') in ('1'):
            for node in doc.xpath("//field[@name='n_supplier']"):
                doc.remove(node)
        if context.get('default_supplier', '0') in ('1'):
            for node in doc.xpath("//field[@name='n_client']"):
                doc.remove(node)

        res['arch'] = etree.tostring(doc)
    return res

I can't guarantee this will work straight off as I havn't tested it for your case, you might need to tweak some values above. Nor can I guarantee this is the best way to achieve this. But I can say very similar code is working perfectly for me in my situation.

Hope that helps :)

0
Tintu Mon
On 4/11/16, 9:23 AM

For other view like "form view" it works fine

attrs="{'invisible':[('customer','!=',True)]}"

If you use this attrs in Tree view it just hide the data/content which shown in that column. Not the complete column.


So I just tried using this instead of above code:

invisible = "context.get('customer') != True"

And it helped me to hide the complete column from Tree view.

0
Gustavo
On 1/17/14, 9:50 AM

Using the attrs XML attributes to set the invisible attribute of an element is not working. In fact, this attrs will only show or hide the content of the tree view, not the entire column. After searching for some solutions from google, i found this one is working. As an example, i add two new fields on invoice model (called ref_partner_id and sales_inv) and it will shows on the invoice tree view only if the type is out_invoice (supplier invoice):

<field name=”ref_partner_id” string=”Customer (name)” invisible=”context.get(‘type’) == ‘out_invoice’ “/>
<field name=”sales_inv”  invisible=”context.get(‘type’) == ‘out_invoice’ “/>

This will work because we got the context variable that inform us the type of invoices currently being displayed on the tree. This comes from the account module (account_invoice_view.xml), for example on the action_invoice_tree2:

<record id=”action_invoice_tree2″ model=”ir.actions.act_window”>
<field name=”name”>Supplier Invoices</field>
<field name=”res_model”>account.invoice</field>
<field name=”view_type”>form</field>
<field name=”view_mode”>tree,form,calendar,graph</field>
<field eval=”False” name=”view_id”/>
<field name=”domain”>[('type','=','in_invoice')]</field>
    <field name=”context”>{‘default_type’: ‘in_invoice’, ‘type’: ‘in_invoice’, ‘journal_type’: ‘purchase’}</field>
</record>

Source: http://vitraining.com/show-hide-columns-in-openerp-tree-view-xml/

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 5/3/13, 10:57 AM
Seen: 9552 times
Last updated: 4/11/16, 9:23 AM