10694 remove bulk calls

This commit is contained in:
Arthur 2022-10-27 13:49:09 -07:00
parent a176e9452f
commit b609d9074b
2 changed files with 19 additions and 39 deletions

View File

@ -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()
#

View File

@ -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()