Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
3 Răspunsuri
12587 Vizualizări

I'm pretty new to Odoo and Python in general and am trying to add a mod to our helpdesk.  We built a wizard to set up a form to add an IT tech to multiple tickets at once.  I know I should use context.get('active_ids') so I can go through all the selected tickets, but I'm having issues getting it to work.  Any advice would be appreciated

helpdesks_ids = fields.Many2many('helpdesk.ticket')
user_ids = fields.Many2many('res.users', string="IT Technicians", required=True)

@api.multi
def action_assign_tech(self, vals):
      t = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
      for ticket in self.helpdesks_ids:
           t.user_id = ticket.user_id

Imagine profil
Abandonează
Cel mai bun răspuns

First off, "self" is the record generated by the wizard. So when you want to access fields from the wizard use: self.my_field

Second, you should use a Many2one field for user_id, because you can only assign one user to the field helpdesk.ticket.user_id (it's a Many2one field).

You don't have to add the field helpdesks_ids, because you are getting these records using browse on active_ids.

When writing the same values to multiple records, use the write() method instead of a loop. It's faster.

Try this:

user_id = fields.Many2one('res.users', string="IT Technicians", required=True)

@api.multi
def action_assign_tech(self):
    tickets = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
    tickets.write({'user_id': self.user_id.id})
Imagine profil
Abandonează
Cel mai bun răspuns

Hi,

Update your code like this,


@api.multi
def action_assign_tech(self, vals):
      tickets = self.env['helpdesk.ticket'].browse(self._context.get('active_ids'))
      for tic in tickets:
#adjust the below line as per the need
#by tic you can access each tickets
           tic.user_id = tic.user_id

Thanks

Imagine profil
Abandonează
Autor Cel mai bun răspuns

@Marius Stedjan

Very much appreciate your answer! Thanks a bunch!

Just so you know, when I tested it, I first got error "Unable to set NOT NULL on 'user_id'" to which I fixed by getting rid of "required=True".

Then I got error "Can't adapt type 'res.users'", to which I fixed by changing

tickets.write({'user_id': self.user_id})# previously

tickets.write({'user_id': self.user_id.id})# revised

Thanks so much for putting me on the right path!

Imagine profil
Abandonează

You’re welcome!

Sorry about forgetting the .id. It was too late at night, hehe. I’ve updated the answer.

As for the required=True;

I suppose you got this when you didn’t set a user in the wizard? The point of required is to force the user to set a user.. which makes sense in a wizard like this.

Related Posts Răspunsuri Vizualizări Activitate
3
mar. 25
4610
0
mai 21
3749
0
aug. 17
3212
0
mar. 15
4283
1
apr. 25
1965