_rec_name and name_get()



_rec_name = ‘name’ (by default) 

where ‘name’ is field of the model. If ‘name’ field is not there then you have to assign any other filed name to this ‘_rec_name’ variable from columns.

Note : If ‘name’ field is there in your columns then there is no need to specify '_rec_name'. OpenERP takes name field by default.

You have seen name in any form when you select many2one field. For example in Sale Order when you select Customer, you can see Customer's Name in that many2one field. Now if you want to show Customer's Phone Number in many2one field, you have to define phone field in _rec_name like this: _rec_name = 'phone'
If your columns don't have any name field then you have to define any field in _rec_name.


Let’s try to dig more using an example.


Case I:


If you are having hr.employee module with name field and you are using it as reference in other module, say hr.employee.detail to provide other employee details then you will use it as:

'employee_id' : fields.many2one('hr.employee', 'Employee')

and it will show you the employee name in selection list.

But, In a company, more than one employee can share the same name. So name field can’t be used to differentiate them from each other. But, employee code will be unique for each employee. In such cases, we will take a column field like ‘emp_code’ and will assign it to ‘_rec_name’

_rec_name = ‘emp_code’

So that it can show employee code in dropdown instead of employee name.

Note : In both above cases 'id' will only be store in database. If will affect only the display part.

------------------------------------------------------------------------------------

------------------------------------------------------------------------------------

name_get() 

Case II: 


In same above example if your manager asks you to show combination of both i.e Employee Name, Employee Code what you will do?

In this case we will override name_get() method in hr.employee module

  def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['name','emp_code'], context=context)
        res = []
        for record in reads:
            name = record['name']
            if record['emp_code']:
                name = record['emp_code'][1]+' / '+name
            res.append((record['id'], name))
        return res
and it will display combination of both in drop-down.

The name_get method is used to display value of a record in a many2one field.
Default name_get method return the output value "name" field value in the table (i.e many2one field).
suppose in the table contain no "name" field then we can use _rec_name = field_name
Now in name_get method return _rec_name field value.
 

Returns a textual representation for the records in self. By default this is the value of the display_name field.
Returns:    list of pairs (id, text_repr) for each records
Return Type:    list(tuple)

name_get is a function which return list of pair. The pair is the id of the item and the name the item, example [(1, "SO001"), (4, "SO004")].

 There is no special field to display the result of name_get. If you need to put the result of name_get method in a field of a record, you should create with an attribute 'compute' 

class Shivam(osv.osv):
    def name_get(self, cr, uid, ids, context=None):
        if not ids:
            return []
        reads = self.read(cr, uid, ids, ['name','parent_id'], context=context)
        res = []
        for record in reads:
            name = record['name']
            if record['parent_id']:
                name = record['parent_id'][1]+' / '+name
            res.append((record['id'], name))
        return res

    def _name_get_fnc(self, cr, uid, ids, prop, unknow_none, context=None):
        res = self.name_get(cr, uid, ids, context=context)
        return dict(res)
   
    _name='shivam.mahajan'
    _columns={
    'name': fields.char("Technology", required=True),
    'complete_name': fields.function(_name_get_fnc, type="char", string='Name'), # if you want u can show it in too
    'parent_id': fields.many2one('shivam.mahajan', 'Parent Technology', select=True), # By default
    }
Copyright © 2013 SoftKul