How to write many2many on wizard to many2many on a record

Hi,
I am trying to add some records from a many2many on a wizard to a many2many on a record, but with no success.
I had done a wizard with all the model views on it, where there is one like

class AddUsersStart(ModelView):
       'Add User Start'
       __name__ = 'add.users.start'
      (...)
      users = fields.Many2Many('res.user',None,None,'Users')
      (...)  

class AddUserWizard(Wizard):
      'Add User Wizard'
     __name__ = 'add.users.wizard'
    (...)
    def do_add_users(self, action):
        pool = Pool()
        MyClass = pool.get('mymodel.myclass')
        (...)
        myclass = {
                (...)
                'users': self.start.users
                 }
        MyClass.create([myclass])
        (...)

Where the ‘users’ = fields.Many2Many(‘res.user’,‘myclass’,‘user’,‘Users’).
I tried to do it like the [(‘create’,[{ ‘users’: …,}])] on a One2Many, but no success.

Any help would be appreciated. Thanks!
Francisco

The low level operation to add records to a many2many field is to set [('add', <ids>)].
But it is low level and not very nice to deal with. Instead it is simpler to the ActiveRecord design like:

def do_add_users(self, action):
    …
    record = MyClass()
    record.users = self.start.users
    …
    record.save()
1 Like

This is the answer I was looking for. I need to create several records and assign it to a list of users.

This one is simpler, but only if you want to create one record.

Thank you very much! I really appreciate your help
Francisco

You can make a loop and save same at once:

records = []
for …:
   record = MyClass()
   record.users = self.start.users
   records.append(record)
MyClass.save(records)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.