如何在 onchange 函数中为 One2many 字段添加域(domain)?

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

How to add domain in onchange function for a One2many field

问题

了解情况
我有以下两个模型。我根据'requirement_id'添加了一个onchange函数。我想在'panel_id'字段中添加一个基于'requirement_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.html
匿名

发表评论

匿名网友

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

确定