[FIX] mrp: make the unbuild form more scalable
Making a dynamic domain with a `allowed_xxx_ids` field is a bad idea when the domain doesn't filter out the most of records. It can create a performance issue, if the `allowed_xxx_ids` contains too much ids, then a lot of data will be exchange to/from server for each onchange which slows down all the form views. task-2558097
This commit is contained in:
@@ -45,7 +45,7 @@ class MrpUnbuild(models.Model):
|
||||
states={'done': [('readonly', True)]}, check_company=True)
|
||||
mo_id = fields.Many2one(
|
||||
'mrp.production', 'Manufacturing Order',
|
||||
domain="[('id', 'in', allowed_mo_ids)]",
|
||||
domain="[('state', '=', 'done'), ('company_id', '=', company_id), ('product_id', '=?', product_id)]",
|
||||
states={'done': [('readonly', True)]}, check_company=True)
|
||||
mo_bom_id = fields.Many2one('mrp.bom', 'Bill of Material used on the Production Order', related='mo_id.bom_id')
|
||||
lot_id = fields.Many2one(
|
||||
@@ -72,22 +72,6 @@ class MrpUnbuild(models.Model):
|
||||
state = fields.Selection([
|
||||
('draft', 'Draft'),
|
||||
('done', 'Done')], string='Status', default='draft')
|
||||
allowed_mo_ids = fields.One2many('mrp.production', compute='_compute_allowed_mo_ids')
|
||||
|
||||
@api.depends('company_id', 'product_id')
|
||||
def _compute_allowed_mo_ids(self):
|
||||
for unbuild in self:
|
||||
domain = [
|
||||
('state', '=', 'done'),
|
||||
('company_id', '=', unbuild.company_id.id)
|
||||
]
|
||||
if unbuild.product_id:
|
||||
domain = expression.AND([domain, [('product_id', '=', unbuild.product_id.id)]])
|
||||
allowed_mos = self.env['mrp.production'].search_read(domain, ['id'])
|
||||
if allowed_mos:
|
||||
unbuild.allowed_mo_ids = [mo['id'] for mo in allowed_mos]
|
||||
else:
|
||||
unbuild.allowed_mo_ids = False
|
||||
|
||||
@api.onchange('company_id')
|
||||
def _onchange_company_id(self):
|
||||
|
||||
@@ -116,7 +116,6 @@
|
||||
<field name="location_id" options="{'no_create': True}" groups="stock.group_stock_multi_locations"/>
|
||||
<field name="location_dest_id" options="{'no_create': True}" groups="stock.group_stock_multi_locations"/>
|
||||
<field name="has_tracking" invisible="1"/>
|
||||
<field name="allowed_mo_ids" invisible="1"/>
|
||||
<field name="lot_id" attrs="{'invisible': [('has_tracking', '=', 'none')], 'required': [('has_tracking', '!=', 'none')]}" groups="stock.group_production_lot"/>
|
||||
<field name="company_id" groups="base.group_multi_company"/>
|
||||
</group>
|
||||
@@ -155,7 +154,6 @@
|
||||
<field name="location_id" options="{'no_create': True}" groups="stock.group_stock_multi_locations"/>
|
||||
<field name="location_dest_id" options="{'no_create': True}" groups="stock.group_stock_multi_locations"/>
|
||||
<field name="has_tracking" invisible="1"/>
|
||||
<field name="allowed_mo_ids" invisible="1"/>
|
||||
<field name="lot_id" attrs="{'invisible': [('has_tracking', '=', 'none')], 'required': [('has_tracking', '!=', 'none')]}" groups="stock.group_production_lot"/>
|
||||
<field name="company_id" groups="base.group_multi_company" readonly="1"/>
|
||||
</group>
|
||||
|
||||
Reference in New Issue
Block a user