为什么m2m字段域在odoo表单中不工作



Odoo域过滤器对子模型的m2m字段不起作用。假设我有5个模型预算,预算线,部门,团队成员和GLCode。

class Budget(models.Model):
_name = 'mn.budget'
name = fields.Char(string='Name', required=True, tracking=True)
budget_line_ids = fields.One2many('mn.budget.line', 'budget_id', string='Budget Lines',)
department_id = fields.Many2one('mn.department', string='Department',)

class BudgetLine(models.Model):
_name = 'mn.budget.line'
budget_id = fields.Many2one('mn.budget', string='Budget')
gl_code = fields.Many2one('mn.gl_code', string='GL Code',)
team_members = fields.Many2many(related='gl_code.member_ids', string='Team Members')
class Department(models.Model):
_name = 'mn.department'
name = fields.Char(string='Name')
budget_ids = fields.One2many('mn.budget', 'department_id', string='Budgets')
team_member_ids = fields.One2many('mn.team_member', 'department_id', string='Team Members')
class GLCode(models.Model):
_name = 'mn.gl_code'
code = fields.Char(string='GL Code', required=True)
budget_line_ids = fields.One2many('mn.budget.line', 'gl_code', string='Budget Lines')
member_ids = fields.Many2many('mn.team_member', string='Team Members')
class TeamMember(models.Model):
_name = 'mn.team_member'
member_id = fields.Many2one(
'res.users', string='Member',
# domain user type internal user
domain=[('groups_id', '=', 1)]
)
department_id = fields.Many2one('mn.department', string='Department')
gl_codes = fields.Many2many('mn.gl_code', string='GL Codes')

BudgetLine可以从Budget表单页面创建。我想过滤gl_code,其父预算模型department_id与member_ids部门_id或GL Code ID在部门相关的TeamMember gl_codes相同。

我尝试在BudgetLine模型gl_code字段以下域不工作

gl_code = fields.Many2one(
'mn.gl_code', string='GL Code', 
domain=lambda self: [
('id', 'in',  self.budget_id.department_id.team_member_ids.mapped('gl_codes').ids)
]
)

如何解决这个问题?

所使用的域应该评估为空列表,因为self.budget_id是一个空记录集,而您可以使用计算字段

例子:

class Budget(models.Model):
_name = 'mn.budget'
@api.depends('department_id')
def _compute_gl_code_ids(self):
if self.department_id:
self.gl_code_ids = self.department_id.team_member_ids.gl_codes
else:
self.gl_code_ids = []
gl_code_ids = fields.Many2many('mn.gl_code', compute='_compute_gl_code_ids')

在视图定义中添加域:

<form>
<field name="gl_code_ids" invisible="0"/>
<group>
<field name="name"/>
<field name="department_id"/>
</group>
<field name="budget_line_ids">
<tree editable="bottom">
<field name="budget_id"/>
<field name="gl_code" domain="[('id', 'in', parent.gl_code_ids)]"/>
<field name="team_members"/>
</tree>
</field>
</form>

相关内容

  • 没有找到相关文章

最新更新