Odoo many2many复选框默认全选及批量全选实现求助
Odoo Many2many复选框全选解决方案
一、默认全部选中的修复方案
你之前的默认值写法仅指定选中ID为2的单条记录,且未匹配视图中依赖halle_id的动态domain,因此无法实现全选。分两种场景处理:
1. 新建记录时halle_id已有默认值
如果halle_id字段本身有默认值(或通过上下文传入初始值),可修改字段default参数,直接匹配对应domain的所有记录:
event_booth_ids = fields.Many2many('event.booth', string='Stände', default=lambda self: self.env['event.booth'].search([ ('halle_id', '=', self.default_get(['halle_id'])['halle_id']) ]) )
2. halle_id由用户手动选择(动态匹配)
如果halle_id是用户新建记录后手动选择的,需通过onchange方法实现选中所有符合条件的记录:
@api.onchange('halle_id') def onchange_halle_id(self): if self.halle_id: self.event_booth_ids = self.env['event.booth'].search([ ('halle_id', '=', self.halle_id.id) ]) else: self.event_booth_ids = False
二、添加“全选”按钮的实现方案
若需保留手动选择灵活性,同时支持一键全选,按以下步骤操作:
1. 在模型中添加全选方法
def action_select_all_booths(self): for record in self: if record.halle_id: booths = self.env['event.booth'].search([ ('halle_id', '=', record.halle_id.id) ]) record.event_booth_ids = booths
2. 在视图中添加按钮
在对应form视图的合适位置(如字段上方或按钮组)添加按钮:
<button name="action_select_all_booths" string="全选所有展位" type="object" class="oe_highlight"/> <field name="event_booth_ids" widget="many2many_checkboxes" domain="[('halle_id', '=', halle_id)]"/>
点击“全选所有展位”按钮后,系统会自动选中当前halle_id下的所有展位记录。
内容的提问来源于stack exchange,提问作者unnamed-dev
相关产品推荐
相关产品推荐

