在Openerp中使函数字段可编辑?

2024-09-30 01:35:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

当我们创造

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

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

如何使该字段具有可编辑性?在


Tags: storename功能true编辑convertfieldsstring
2条回答

计算字段有一个函数,可以自动计算它在某些源数据上的值。在

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

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)

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

相关问题 更多 >

    热门问题