0
votes

how can I use many2one field in the function?

this is my code:

def _get_unit(self, cr, uid, ids, fields,arg, context=None):
res = {}
list_data = []
for record in self.browse(cr, uid, ids,unit):
    list_data.append(record[unit.id])
    return super(learning_course, self)._get_unit(cr, uid, ids, context=context)

_columns = {

'unit': fields.many2one('hr.department', 'unit'),

'department': fields.function(_get_unit, string='department' , store=True ,type='many2one' ,relation='hr.department'),

}

def onchange_user(self, cr, uid, ids, user_id, context=None):
    unit = False
    if user_id:
        unit = self.pool.get('res.users').browse(cr, uid, user_id, context=context).context_department_id.id
        return {'value': {'unit' : unit  }}
    return {'value': {} }

but I get this error:

for record in self.browse(cr, uid, ids,unit): AttributeError: 'browse_record_list' object has no attribute 'id'

what should I do?

1
hi,thank s for your reply. 'user_id': fields.many2one('res.users', 'user', readonly=True),m.safari
I just use user_id in onchange function.i think it is still many2onem.safari
Then it is an integer.unit is undefined in _get_unit function.Kenly

1 Answers

0
votes
def _get_unit(self, cr, uid, ids, prop, unknow_none, context=None):
res = {}
for record in self.browse(cr, uid, ids):
    res [record.id] = record.user_id.context_department_id.id
    return res

_columns = {
'user_id': fields.many2one('res.users', 'user', readonly=True),
'unit_id': fields.function(_get_unit, string='dep' , store=True ,type='many2one',relation='hr.department'),

}