From 05f75e1366d507ad62808bf5f2d783b2158bea53 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Thu, 6 Apr 2023 16:48:12 -0400 Subject: [PATCH] Fixes #12118: Refactor bulk creation logic under _instantiate_components() --- netbox/dcim/models/devices.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 1429003c55a..0526c49cb66 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -774,8 +774,10 @@ def _instantiate_components(self, queryset, bulk_create=True): bulk_create: If True, bulk_create() will be called to create all components in a single query (default). Otherwise, save() will be called on each instance individually. """ - components = [obj.instantiate(device=self) for obj in queryset] - if components and bulk_create: + if bulk_create: + components = [obj.instantiate(device=self) for obj in queryset] + if not components: + return model = components[0]._meta.model model.objects.bulk_create(components) # Manually send the post_save signal for each of the newly created components @@ -788,8 +790,9 @@ def _instantiate_components(self, queryset, bulk_create=True): using='default', update_fields=None ) - elif components: - for component in components: + else: + for obj in queryset: + component = obj.instantiate(device=self) component.save() def save(self, *args, **kwargs):