User Contacts filtered by department

Asked by Jesús Escribano

Hi everyone.
I'm starting with oerp and I'm trying to filter user contacts, personaly and by department. I have created a record rule like that: ['|',('create_uid','=',user.id),('user_depart_id','in',[user.id])].
The first part is just to filter the contacts created by the user and it works fine. The second part is where I have troubles. I want to save in a field, a list of the users id, from the departments of the creator user to finally compare with the user.id.

I'm working in a new class inherited from base_contact. To save the members of the user departments, first I use "browse" to get all the fields from the res.users table from the user creator. Then I use "browse" again with hr.department table to get the fild "member_ids" from this, and this way to save the users list from those departments.
Let me show you the code: I created a many2many related field with departments in res.users table named 'department_ids', like this:
class departments_user(osv.osv):
    _inherit = 'res.users'
    _columns = {
        'department_ids':fields.many2many('hr.department', 'hr_department_user_rel', 'user_id', 'department_id', 'Departments'),
}
departments_user()

Also I created a field to save the department members list of the user:

'dep_aux':fields.many2many('res.users', 'members_user_rel', 'name', 'uid', 'Users in departments', readonly=False),

This is the create function, where I'm trying to get list of members:

def create(self, cr, uid, vals, context=None):
        user = self.pool.get('res.users').browse(cr, uid, uid) ------> result: browse_record(res.users, 4) OK
        departments = self.pool.get('hr.department').browse(cr, uid, [x.id for x in user.department_ids]) ----->[browse_record(hr.department, 3), browse_record(hr.department, 4)] OK, the user is in two departments.
        vals['dep_aux'] = [x.member_ids for x in departments] ---> If I print that the result is: [[browse_record(res.users, 4)], [browse_record(res.users, 4), browse_record(res.users, 5)]] OK I have 2 users, 4 is in two departments and 5 in one of them with user 4.

        cnt = super(res_partner_contact,self).create(cr, uid, vals, context)

        return cnt

But when It has to save, I have an error: "AttributeError: 'int' object has no attribute '_table_name'"

I think that the mistake is the definition of the field dep_aux. What do you think? How would you do this?

First I thought in save the departments of the contact creator, and then compare with the departments of the current user, I think that this is more elegant, but I don't have any idea of how to do that.

Thank you very much for your time and your help. Thanks a lot.

Question information

Language:
English Edit question
Status:
Solved
For:
OpenERP Edit question
Assignee:
No assignee Edit question
Solved by:
Vinay Rana (OpenERP)
Solved:
Last query:
Last reply:
Revision history for this message
Best Vinay Rana (OpenERP) (vra-openerp) said :
#1

Hello Jesús Escribano,

For assigning many2many field you need to follow following format:
=>[(6,0,[ids])].

You need to separate the ids from [[browse_record(res.users, 4)], [browse_record(res.users, 4), browse_record(res.users, 5)]] list and create one single list => [4,5]

Hope this will help you.
Thanks.

Revision history for this message
Jesús Escribano (jesxr44) said :
#2

Thanks vra (openerp), that solved my question.