Search code examples
pythonpython-2.7odooopenerp-7

Make a functional field editable in Openerp?


How to make functional field editable in Openerp?

When we create

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

This will be displayed has read-only and we can't able to edit the text.

How we make this field has editable?


Solution

  • You must add a inverse function to make the field editable. This parameter is called fnct_inv in OpenERP v7. An example:

    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,
        ),
    }