2015-04-07 21 views
0

我需要顯示在樹視圖從one2many字段值,所以我決定創建功能領域,並宣佈它在我的XML樹視圖:顯示fields.function

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 

和我場的聲明:

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

XML示例代碼:

<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> 

在我的控制檯,我可以看到正確的記錄打印,但我在我的樹視圖中不顯示任何內容。

任何人都可以幫助我嗎?

回答

1

從功能字段刪除商店屬性

+0

你的回答是正確的...嘗試向OP解釋更多爲什麼'store = True'不起作用。所以它會對你遇到的其他人有用 – danidee

+0

http://stackoverflow.com/questions/16185195/openerp-fields-function-explanation/16206895#16206895 – OmaL

0

這只是因爲代碼中的錯誤縮進。請嘗試以下內容:

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 

您已將res[record.id]放在循環的外側,就是這樣。

+0

文件「/home/odoo/openerp/models.py」第5219行,在ensure_one中 raise except_orm(「ValueError」,「Expected singleton:%s」%self) except_orm :('ValueError','Expected singleton:product.template(8,4,5,9,2, 3,7)') –

+0

如果我使用這個函數,我得到這個錯誤,你知道它爲什麼可能嗎? –