Skip to content

Commit 3b3511c

Browse files
padthaitofuhotjeremystretch
authored andcommitted
Refactor 32264ac to re-separate bulk and single device creation. Fixes #15598.
1 parent da13fa5 commit 3b3511c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

netbox/dcim/models/devices.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -996,17 +996,16 @@ def _instantiate_components(self, queryset, bulk_create=True):
996996
bulk_create: If True, bulk_create() will be called to create all components in a single query
997997
(default). Otherwise, save() will be called on each instance individually.
998998
"""
999-
components = [obj.instantiate(device=self) for obj in queryset]
1000-
if not components:
1001-
return
1002-
1003-
# Set default values for any applicable custom fields
1004999
model = queryset.model.component_model
1005-
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
1006-
for component in components:
1007-
component.custom_field_data = cf_defaults
10081000

10091001
if bulk_create:
1002+
components = [obj.instantiate(device=self) for obj in queryset]
1003+
if not components:
1004+
return
1005+
# Set default values for any applicable custom fields
1006+
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
1007+
for component in components:
1008+
component.custom_field_data = cf_defaults
10101009
model.objects.bulk_create(components)
10111010
# Manually send the post_save signal for each of the newly created components
10121011
for component in components:
@@ -1019,7 +1018,11 @@ def _instantiate_components(self, queryset, bulk_create=True):
10191018
update_fields=None
10201019
)
10211020
else:
1022-
for component in components:
1021+
for obj in queryset:
1022+
component = obj.instantiate(device=self)
1023+
# Set default values for any applicable custom fields
1024+
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
1025+
component.custom_field_data = cf_defaults
10231026
component.save()
10241027

10251028
def save(self, *args, **kwargs):

0 commit comments

Comments
 (0)