From b609d9074ba1410a5c71cf7679daa23e62f32570 Mon Sep 17 00:00:00 2001 From: Arthur Date: Thu, 27 Oct 2022 13:49:09 -0700 Subject: [PATCH] 10694 remove bulk calls --- netbox/dcim/models/devices.py | 52 +++++++++------------------- netbox/extras/models/customfields.py | 6 ++-- 2 files changed, 19 insertions(+), 39 deletions(-) diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index d4646762f..5b828a06e 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -831,6 +831,10 @@ class Device(NetBoxModel, ConfigContextModel): 'vc_position': "A device assigned to a virtual chassis must have its position defined." }) + def _create_from_templates(self, qs): + for item in qs: + obj = item.instantiate(device=self).save() + def save(self, *args, **kwargs): is_new = not bool(self.pk) @@ -842,36 +846,16 @@ class Device(NetBoxModel, ConfigContextModel): # If this is a new Device, instantiate all of the related components per the DeviceType definition if is_new: - ConsolePort.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.consoleporttemplates.all()] - ) - ConsoleServerPort.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.consoleserverporttemplates.all()] - ) - PowerPort.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.powerporttemplates.all()] - ) - PowerOutlet.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.poweroutlettemplates.all()] - ) - Interface.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.interfacetemplates.all()] - ) - RearPort.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.rearporttemplates.all()] - ) - FrontPort.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.frontporttemplates.all()] - ) - ModuleBay.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.modulebaytemplates.all()] - ) - DeviceBay.objects.bulk_create( - [x.instantiate(device=self) for x in self.device_type.devicebaytemplates.all()] - ) - # Avoid bulk_create to handle MPTT - for x in self.device_type.inventoryitemtemplates.all(): - x.instantiate(device=self).save() + self._create_from_templates(self.device_type.consoleporttemplates.all()) + self._create_from_templates(self.device_type.consoleserverporttemplates.all()) + self._create_from_templates(self.device_type.powerporttemplates.all()) + self._create_from_templates(self.device_type.poweroutlettemplates.all()) + self._create_from_templates(self.device_type.interfacetemplates.all()) + self._create_from_templates(self.device_type.rearporttemplates.all()) + self._create_from_templates(self.device_type.frontporttemplates.all()) + self._create_from_templates(self.device_type.modulebaytemplates.all()) + self._create_from_templates(self.device_type.devicebaytemplates.all()) + self._create_from_templates(self.device_type.inventoryitemtemplates.all()) # Update Site and Rack assignment for any child Devices devices = Device.objects.filter(parent_bay__device=self) @@ -1041,7 +1025,6 @@ class Module(NetBoxModel, ConfigContextModel): ("frontporttemplates", "frontports", FrontPort) ]: create_instances = [] - update_instances = [] # Prefetch installed components installed_components = { @@ -1059,15 +1042,12 @@ class Module(NetBoxModel, ConfigContextModel): if existing_item: # Assign it to the module existing_item.module = self - update_instances.append(existing_item) + existing_item.save() continue # Only create new components if replication is enabled if not disable_replication: - create_instances.append(template_instance) - - component_model.objects.bulk_create(create_instances) - component_model.objects.bulk_update(update_instances, ['module']) + template_instance.save() # diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index 2de806ca6..0a1c58254 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -198,7 +198,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge instances = model.objects.exclude(**{f'custom_field_data__contains': self.name}) for instance in instances: instance.custom_field_data[self.name] = self.default - model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100) + instance.save() def remove_stale_data(self, content_types): """ @@ -210,7 +210,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge instances = model.objects.filter(**{f'custom_field_data__{self.name}__isnull': False}) for instance in instances: del instance.custom_field_data[self.name] - model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100) + instance.save() def rename_object_data(self, old_name, new_name): """ @@ -222,7 +222,7 @@ class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLogge instances = model.objects.filter(**params) for instance in instances: instance.custom_field_data[new_name] = instance.custom_field_data.pop(old_name) - model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100) + instance.save() def clean(self): super().clean()