如何在One2many字段的onchange函数中添加域名?

huangapple go评论54阅读模式
英文:

How to add domain in onchange function for a One2many field

问题

理解情况
我有下面两个模型。我根据'requirement_id'添加了一个onchange函数。我想在'requirement_id'字段中添加一个域过滤器,以从'requirement_id'中过滤'panel_id'字段。这是否可能?我的onchange函数以这种方式不起作用。

class PanelVendorAssignment(models.Model):
    _name = "panel.vendor.assignment"
    requirement_id = fields.Many2one('hr.job', string='Requirement')
    panel_vendor_assignments_ids = fields.One2many(
        string='Vendor', comodel_name='panel.vendor.assignment.line',
        inverse_name='panel_vendor_assignment_id', tracking=True)

    @api.onchange('requirement_id')
    def _onchange_panel_stage(self):
        if self.requirement_id:
            panel_stage_ids = self.env['panel.time.slot.availability'].sudo().search(
                [('requirement_id', '=', self.requirement_id.id), ('status', '=', 'open')])
            for rec in panel_stage_ids:
                domain = [('id', '=', rec.panel_user_id.id)]
                return {'domain': {'panel_vendor_assignments_ids.panel_id': domain}}

class PanelVendorAssignmentLine(models.Model):
    _name = 'panel.vendor.assignment.line'
    panel_vendor_assignment_id = fields.Many2one(comodel_name='panel.vendor.assignment', ondelete='cascade')
    panel_id = fields.Many2one(comodel_name='res.partner', required=True)

请注意,我已经保留了代码的格式和结构,只翻译了您提供的内容。

英文:

Understand the situation
I 've 2 model below. I have added a onchange function based on 'requirement_id' . I want to add a domain filter in panel_id field from requirement_id . is it possible to do? my onchange function in this way it's not working

class PanelVendorAssignment(models.Model):
	_name = "panel.vendor.assignment"
	requirement_id = fields.Many2one('hr.job', string='Requirement')
	panel_vendor_assignments_ids = fields.One2many(
        string='Vendor', comodel_name='panel.vendor.assignment.line',
        inverse_name='panel_vendor_assignment_id', tracking=True)

	@api.onchange('requirement_id')
	def _onchange_panel_stage(self):
		if self.requirement_id:
			panel_stage_ids = self.env['panel.time.slot.availability'].sudo().search(
            [('requirement_id', '=', self.requirement_id.id),('status', '=', 'open')])
        		for rec in panel_stage_ids:
				domain = [('id', '=', rec.panel_user_id.id)]
            		return {'domain': {'panel_vendor_assignments_ids.panel_id': domain}}

class PanelVendorAssignmentLine(models.Model):
	_name = 'panel.vendor.assignment.line'
	panel_vendor_assignment_id = fields.Many2one(comodel_name='panel.vendor.assignment', ondelete='cascade')
	panel_id = fields.Many2one(comodel_name='res.partner',required=True,)

答案1

得分: 0

不能这样做。

你的代码也有一些错误,会覆盖域的值,因此你将返回每个记录的最后一个域,而不是按记录的域。

你可以做的是计算并保存o2m中每个记录的域,保存在另一个字段中,以供panel_id字段在本地使用。

要保存域,你可以使用二进制字段或使用这个模块:

https://github.com/OCA/web/tree/15.0/web_domain_field

英文:

No you can't do that.

Your code also have some errors overwriting the value of the domain so you will be returning the last domain not per record domain

What you can do it's to calculate and save the domain for every record in the o2m in another field to be used locally by the panel_id field

To save the domain you could use a binary field or use this module:

https://github.com/OCA/web/tree/15.0/web_domain_field

huangapple
  • 本文由 发表于 2023年8月9日 13:41:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864872-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定