其实我想做一个 Many2many 字段动态
allowed_economic_activity_ids = fields.Many2many(
'izi.economic.activity', string='Allowed Economic Activities' domain=_filtrar_actividades)
@api.onchange('allowed_economic_activity_ids','invoice_line_ids')
def _filtrar_actividades(self):
if self:
query = self.env['izi.economic.activity'].search([('id','!=','1')])
if query:
domain = [('id', 'in', query.ids )]
return domain
添加 @api.onchage 时不起作用并显示错误“attributeerror: 'list' object has no attribute 'get'” 当点击 allowed_economic_activity_ids 字段或 invoice_line_ids 时,many2many 字段的值应该改变
您可以在_onchange_eval文档字符串中看到以下内容:
同时域和警告信息被放入字典中
result
Odoo 期望 onchange 函数返回字典而不是列表。
您可以定义一个计算字段,然后像在 OCA 中那样在字段域中使用它base_tier_validation
reviewer_field_id = fields.Many2one(
comodel_name="ir.model.fields",
string="Reviewer field",
domain="[('id', 'in', valid_reviewer_field_ids)]",
)
valid_reviewer_field_ids = fields.One2many(
comodel_name="ir.model.fields",
compute="_compute_domain_reviewer_field",
)
@api.depends("review_type", "model_id")
def _compute_domain_reviewer_field(self):
for rec in self:
rec.valid_reviewer_field_ids = self.env["ir.model.fields"].search(
[("model", "=", rec.model), ("relation", "=", "res.users")]
)