【问题标题】:Use domain in act_window .Odoo 14在 act_window .Odoo 14 中使用域
【发布时间】:2021-12-30 12:43:25
【问题描述】:

我使用'stock.picking' 模型我在表单视图中添加了一个按钮。 当我单击此按钮时,我想在 'helpdesk.ticket' 模型中创建一个新记录,字段域为 'product_id' 与库存移动行中的产品列表相同。

这是我的代码,我用 domain 试过了,但是不行。

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)],
    }

有什么帮助吗?

谢谢。

【问题讨论】:

    标签: python odoo


    【解决方案1】:

    你可以通过上下文传递product_ids

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

    并使用函数计算product_id 字段域:

    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)
    

    【讨论】:

    • 当我尝试这个时,我在 product_id 中得到了空域,尽管在打印 context.get('product_ids', []) 时我有 2 个产品 id
    • 我更新了答案以在 product_ids 不可用时返回默认域(在表单视图中创建票证)。我在 OCA helpdesk_mgmt 模型上对其进行了测试,当使用 create_ticket 按钮打开表单视图时,产品被过滤
    • 以前的域定义一般用在向导中
    • 它适用于您的解决方案,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 2021-02-21
    • 2021-03-06
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多