如何在自定义模块中为Odoo中的产品类别中使用的自定义层次结构创建complete_name字段?

时间:2016-09-19 14:52:51

标签: odoo-9

我正在尝试创建一个字段“complete_name”,它显示的层次结构名称与产品类别网格上的内容类似,但我似乎无法使其工作。当我使用新字段“complete_name”访问相关视图时,它只是将Odoo置于无限加载屏幕中。

我尝试复制addons/product/product.py中使用的代码并使用compute而不是.function类型迁移到使用Odoo 9 API,但它不起作用。 有人能帮我理解什么是错的吗?下面是我的模型类,在我的视图中没有complete_name字段,工作正常。

class cb_public_catalog_category( models.Model ):
    _name = "cb.public.catalog.category" 
    _parent_store = True     
    parent_left = newFields.Integer( index = True ) 
    parent_right = newFields.Integer( index = True )
    name = newFields.Char( string = 'Category Name' ) 
    child_id = newFields.One2many( 'catalog.category', 'parent_id', string = 'Child Categories' ) 
    complete_name = newFields.Char( compute = '_name_get_fnc', string = 'Name' )

def _name_get_fnc( self ):
    res = self.name_get( self ) 
    return dict( res )

2 个答案:

答案 0 :(得分:0)

您的计算函数应该定义类的属性值,而不是返回值。确保您分配的值complete_name是一个字符串。

name_get()也返回一个元组。我不确定你是否真的想要这个元组的字符串表示,或者只是实际的名称值。

试试这个

def _name_get_fnc( self ):
    self.complete_name = self.name_get()[1]

如果您真的想要name_get()返回的内容,请尝试此操作。

def _name_get_fnc( self ):
    self.complete_name = str(self.name_get())

如果您仍然遇到问题,我会合并一些日志记录,以便更好地了解您将complete_name的值设置为。

import logging
_logger = logging.getLogger(__name__)

def _name_get_fnc( self ):
    _logger.info("COMPUTING COMPLETE NAME")
    _logger.info("COMPLETE NAME: " + str(self.name_get()))
    self.complete_name = self.name_get()

如果这不能说明问题是什么,那么你总是可以尝试静态地为它分配一个值,使你的视图出现问题。

def _name_get_fnc( self ):
    self.complete_name = "TEST COMPLETE NAME" 

答案 1 :(得分:0)

经过进一步审查后,我想我能回答自己的问题。事实证明,很多事情都非常简单。

只需使用“_inherit”并继承product.category即可 模型。这样可以访问所有功能和字段 product.category包括complete_name字段 并从我的自定义模型数据计算名称。我曾是 能够删除我的_name_get_func并只使用继承 功能

最终的模型定义如下。一旦这个 更新完成我能够添加“complete_name”字段 在我看来,结果是所希望的!

class cb_public_catalog_category( models.Model ):
     _name = "cb.public.catalog.category" 
     _inherit = 'product.category'
     _parent_store = True     
     parent_left = newFields.Integer( index = True ) 
     parent_right = newFields.Integer( index = True )
     name = newFields.Char( string = 'Category Name' ) 
     child_id = newFields.One2many( 'catalog.category', 'parent_id', string = 'Child Categories' )