Use domain in act_window .Odoo 14

Viewed 240

I worked with 'stock.picking' model I added a button in the form view. When I click in this button , I want to create a new record in 'helpdesk.ticket' model , with domain of field 'product_id' same of list of products in stock move lines.

Here is my code , I tried it with domain , but doesn't work .

def create_ticket(self):
    helpdesk_ticket_id = self.env['helpdesk.ticket'].create({
        'name': 'Ticket'
    })
    return {
        'name': _('New'),
        'view_mode': 'tree,form',
        'res_model': 'helpdesk.ticket',
        'views': [(self.env.ref('helpdesk.helpdesk_ticket_view_form').id, 'form'),
                  (self.env.ref('helpdesk.helpdesk_tickets_view_tree').id, 'tree'),
                  ],
        'type': 'ir.actions.act_window',
        'res_id': helpdesk_ticket_id.id,
        'domain': [('product_id', 'in', self.move_ids_without_package.product_id)],
    }

Any help please?

Thanks.

1 Answers

You can pass product_ids through the context:

'context': {'product_ids': self.move_ids_without_package.product_id.ids},

and use a function to compute the product_id field domain:

class HelpDeskTicket(models.Model):
    _inherit = 'helpdesk.ticket'

    @api.model
    def _get_product_domain(self):
        if 'product_ids' in self.env.context:
            return [('id', 'in', self.env.context['product_ids'])]
        return []

    product_id = fields.Many2one(domain=_get_product_domain)
Related