在 Openerp 中使功能字段可编辑?

Make a functional field editable in Openerp?

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

当我们创建

'capname': fields.function(
    _convert_capital, string='Display Name', type='char', store=True
),

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

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

您必须添加反函数才能使字段可编辑。这个参数在 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,
    ),
}

计算字段具有自动计算其在某些源数据上的值的功能。

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

来自documentation

to allow setting values on a computed field, use the inverse parameter. It is the name of a function reversing the computation and setting the relevant fields:

示例代码:

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)