Tin*_*n M 6 python python-2.7 openerp-7 odoo
如何在Openerp中编辑功能字段?
当我们创造
'capname': fields.function(
_convert_capital, string='Display Name', type='char', store=True
),
Run Code Online (Sandbox Code Playgroud)
这将显示为只读,我们无法编辑文本.
我们如何使这个领域具有可编辑性?
计算字段具有自动计算某些源数据的值的函数.
可以添加逆操作,根据手动设置的值更新源数据,从而使其可编辑.
从文档:
要允许在计算字段上设置值,请使用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)
Run Code Online (Sandbox Code Playgroud)
您必须添加反函数才能使该字段可编辑。该参数在 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,
),
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3715 次 |
| 最近记录: |