Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
47438 Vistas

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.

Avatar
Descartar
Mejor respuesta

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.

Avatar
Descartar
Autor

I'm editing my answer..

Autor Mejor respuesta

@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...?

Avatar
Descartar
Autor

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.

Autor

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

Autor

The resolution above worked.