[IMP] model: optimize setup_models (#18300)
When setting up models, retrieve all manual fields in a single and specific query. This makes the method `_add_manual_fields` 20 times faster.
This commit is contained in:
@@ -791,6 +791,19 @@ class IrModelFields(models.Model):
|
||||
records = self.browse([fields_data.pop(name)['id'] for name in extra_names])
|
||||
records.with_context(**{MODULE_UNINSTALL_FLAG: True}).unlink()
|
||||
|
||||
@tools.ormcache()
|
||||
def _all_manual_field_data(self):
|
||||
cr = self._cr
|
||||
cr.execute("SELECT * FROM ir_model_fields WHERE state='manual'")
|
||||
result = defaultdict(dict)
|
||||
for row in cr.dictfetchall():
|
||||
result[row['model']][row['name']] = row
|
||||
return result
|
||||
|
||||
def _get_manual_field_data(self, model_name):
|
||||
""" Return the given model's manual field data. """
|
||||
return self._all_manual_field_data().get(model_name, {})
|
||||
|
||||
def _instanciate_attrs(self, field_data):
|
||||
""" Return the parameters for a field instance for ``field_data``. """
|
||||
attrs = {
|
||||
@@ -819,7 +832,7 @@ class IrModelFields(models.Model):
|
||||
if not self.pool.loaded and not (
|
||||
field_data['relation'] in self.env and (
|
||||
field_data['relation_field'] in self.env[field_data['relation']]._fields or
|
||||
field_data['relation_field'] in self._existing_field_data(field_data['relation'])
|
||||
field_data['relation_field'] in self._get_manual_field_data(field_data['relation'])
|
||||
)):
|
||||
return
|
||||
attrs['comodel_name'] = field_data['relation']
|
||||
@@ -847,7 +860,7 @@ class IrModelFields(models.Model):
|
||||
|
||||
def _add_manual_fields(self, model):
|
||||
""" Add extra fields on model. """
|
||||
fields_data = self._existing_field_data(model._name)
|
||||
fields_data = self._get_manual_field_data(model._name)
|
||||
for name, field_data in fields_data.iteritems():
|
||||
if name not in model._fields and field_data['state'] == 'manual':
|
||||
field = self._instanciate(field_data)
|
||||
|
||||
Reference in New Issue
Block a user