在Openerp中编辑功能字段?

时间:2015-07-07 11:51:01

标签: python python-2.7 odoo openerp-7

如何在Openerp中编辑功能字段?

当我们创建

@(time(nil)).stringValue);

这将显示为只读,我们无法编辑文本。

我们如何使此字段具有可编辑性?

2 个答案:

答案 0 :(得分:6)

计算字段具有自动计算某些源数据的值的函数。

可以添加逆操作,根据手动设置的值更新源数据,从而使其可编辑。

来自documentation

  

允许在计算字段上设置值,请使用inverse参数。它是反转计算和设置相关字段的函数的名称:

示例代码:

document = fields.Char(compute='_get_document', inverse='_set_document')

def _get_document(self):
    for record in self:
        with open(record.get_document_path) as f:
            record.document = f.read()
def _set_document(self):
    for record in self:
        if not record.document: continue
        with open(record.get_document_path()) as f:
            f.write(record.document)

答案 1 :(得分:2)

您必须添加反函数才能使字段可编辑。此参数在OpenERP v7中称为fnct_inv。一个例子:

def _get_test(self, cr, uid, ids, name, args=None, context=None):
    result = dict.fromkeys(ids, False)
    for line in self.browse(cr, uid, ids, context=context):
        if line.test:
            result[line.id] = line.test
    return result       

def _set_test(self, cr, uid, id, field_name, field_value, args=None, context=None):
    obj = self.browse(cr, uid, id)
    for record in obj:
        if record.test != field_value:
            # The record already exists

            ...

            cr.execute(
                'UPDATE your_table '
                'SET test=%s '
                'WHERE id=%s', (field_value, id)
            )
        else:
            # It is a new record 
            # (or the value of the field was not modified)

    return True

_columns = {
    'test': fields.function(
        string='String for testing', 
        fnct=_get_test, 
        fnct_inv=_set_test,            
        type='char', 
        size=50, 
        store=True,
    ),
}