简体   繁体   中英

Display fields.function on tree view odoo

I need to display values from one2many field in tree view, so I decided to create functional field and declared it on my xml tree view:

def get_product_brands(self, cr, uid, ids, fields, arg, context):
    res={}
    for record in self.browse(cr, uid, ids, context=None).application_data_product_template_ids:
        brands = record.brand.name or ''
    print brands
    res[record.id] = brands
    return res

and my field declaration:

'brands' : fields.function(get_product_brands, method=True, string="Product brands", type='char', store=True)

xml sample code:

<record model="ir.ui.view" id="product_tree_inherit">
    <field name="name">product.tree.inherit</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_tree_view"/>
    <field name="arch" type="xml">
         <xpath expr="//field[@name='categ_id']" position="after"> 
             <field name="brands"/>
         </xpath>
    </field>
</record>

In my console, I can see correct records printed but I does not display anything in my tree view.

Can anyone help me?

从功能字段中删除商店属性

It's only because wrong indentation in code. Try following,

def get_product_brands(self, cr, uid, ids, fields, arg, context):
    res={}
    for record in self.browse(cr, uid, ids, context=None).application_data_product_template_ids:
        brands = record.brand.name or ''
        print brands
        res[record.id] = brands
    return res

You have placed res[record.id] out side the loop, that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM