Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

many2many fields - how to implement

By
karlos
on 9/9/13, 3:36 PM 2,014 views

I have the table: Employees, which contains only the field 'name' and 'login' (it's associated to the table users)

class Employees(osv.osv):
    _name = 'Employees.Employees'
    _columns = {
         'the_name':fields.char('Name:', size = 50),
         'user_id': fields.many2one('res.users', 'User:', select = True),
     }
Employees()

Example:

'John' -> 'John@gmail.com'

'Marilyn' -> 'marilyn@outlook.com'

Then I have the table: Tasks, which contains the fields 'name', 'description', and 'employees_id'. When a task is created it could contain several employees for the same task, that's why I choose the "many2many" because I could select several employees. So, I have tried the following:

class tasks(osv.osv):
    _name = 'tasks.tasks'
    _columns = {
          'the_name':fields.char('Name', size = 50),
          'description':fields.text('Description'),
          'employees_id':fields.many2many('Employees.Employees', 'Employees', '???', 'user_id', 'All Employees')
     }
tasks()

Example:

'Carry sand' -> 'Carry the whole sand from the beach' -> 'john@gmail.com; marilyn@outlook.com'

'Play' -> 'Play with legos' -> 'john@gmail.com'

But I don't know what to put on "???"..thanks.

1

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 9/9/13, 4:49 PM

You have to do this:

'employees_ids': fields.many2many('Employees.Employees', 'tasks_employees_rel', 'task_id', 'employee_id', 'Employees assigned to task')

To give you a better example.

A employee can be assigned to many tasks

A task can be assigned to many employees

So you have a many2many relation, so that means that you have to create a new table containing both keys.

That what you do with this. You say that you are having a many2many relation with employees so you have to write the first arg the other object name.

Then you have to write which table is going to contain the foreign keys, so you write some descriptive table.

Then you need to give a name to your first key, since you are calling it from tasks you have to write tasks_id.

And then you need to give a name to the other object key.

Finally you have to give the field a name, and that's all.

I'm editing my answer..

karlos
on 9/10/13, 6:25 AM
0
karlos
On 9/10/13, 6:36 AM

@Grover Menacho, I was quite sure I needed to create a third table, thanks for warning me that, but here's my doubts.

1) Should I create the third table by code dynamically (if so, how?) or I can go to "pgAdmin III" (postgreSQL management) and create by hand? I think the best approach is dynamically, but I don't know how to create it dynamically to execute in the load of the module.

2) So, with the third table I should have something like this:

  class Employees(osv.osv):
                    _name = 'Employees.Employees'
                    _columns = {
                         'the_name':fields.char('Name:', size = 50),
                         'user_id': fields.many2one('res.users', 'User:', select = True),
                     }
                Employees()

-> Table 'Employees' -> fields: the_name, user_id

class tasks(osv.osv):
    _name = 'tasks.tasks'
    _columns = {
          'the_name':fields.char('Name', size = 50),
          'description':fields.text('Description'),
          'employees_id':fields.many2many('Employees.Employees', 'tasks_employees_rel',  'employee_id', 'task_id', 'All Employees')
    } tasks()

-> table 'Tasks' -> fields: the_name, description (I think employees_id isn't created in the database..Am I wrong??)

And then I need to have a third table, called: tasks_employees_rel, which contains: id, employee_id, task_id...?

I've tried this, and I think it worked..because in the database, table 'tasks_employees_rel', the data is being insert. However, I want to make a rule for this. The user A can only view the tasks assigned to his own profile employee. Remember that, whenever I create a employee I assign them a user.

I was trying something like: [('tasks_employees_rel.employee_id, '=', user.id)], without success.

karlos
on 9/10/13, 7:01 AM

I think I'd just solved this too. I will post my resolution later if I'm 100% sure about it. Thanks.

karlos
on 9/10/13, 7:17 AM

The resolution above worked.

karlos
on 9/10/13, 12:12 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 9/9/13, 3:36 PM
Seen: 2014 times
Last updated: 3/16/15, 8:10 AM