Monday, November 18, 2013

Creates a functional field that save field value in database

I have a field name as :
 _column {  
   'name': fields.char('Name', size=64, required=True, translate=True),  
   'hash_value': fields.function(_get_hash,method=True,type='char',string='Hash Value', store=True )  
 }  

I want hash_value equal to the slugify value of name and also want to save it in database. This code will do the trick.
  def _get_hash(self, cr, uid, ids, fld_name, arg, context=None):  
     if not len(ids):  
       return []  
     result={}  
     reads = self.read(cr, uid, ids, ['name'], context=context)  
     for atr in reads:  
       result[ atr['id'] ] = slugify(atr['name'])  
     return result  

No comments:

Post a Comment