Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 211
Tintumon M Avatar asked Jul 07 '15 11:07

Tintumon M


People also ask

What is the use of write method in Odoo?

Use of Write Method in Odoo: Write method executes an SQL update query on the records. The values of the field  that are going to be wrriten can be found in the vals dictionary, which is the parameter to the method.

What is inverse in Odoo?

The Odoo inverse method, as the name says, performs the inverse of the compute function: the invoked records have particular values for a field, to which you must apply the necessary changes on the field dependencies such that the computation gives us back the expected value. By default, compute fields are read-only.

What is field in Odoo?

Simple Types are Integer, Char, String, etc. Relation Types represent the relations between objects like Many2one, One2many, and Many2many. Functional fields are not stored in the database. They are special fields because the fields are calculated in real-time based on other fields of the view.


2 Answers

A computed field has a function that automatically calculates it's value on some source data.

It is possible to add it the inverse operation, updating the source data based on a value manually set on it, thus making it editable.

From the 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:

Example code:

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)
like image 178
Daniel Reis Avatar answered Oct 17 '22 20:10

Daniel Reis


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,
    ),
}
like image 23
ChesuCR Avatar answered Oct 17 '22 21:10

ChesuCR