Skip to content

Commit

Permalink
Refactor 32264ac to re-separate bulk and single device creation. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
padthaitofuhot authored and jeremystretch committed Apr 4, 2024
1 parent da13fa5 commit 3b3511c
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions netbox/dcim/models/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -996,17 +996,16 @@ 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 not components:
return

# Set default values for any applicable custom fields
model = queryset.model.component_model
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
for component in components:
component.custom_field_data = cf_defaults

if bulk_create:
components = [obj.instantiate(device=self) for obj in queryset]
if not components:
return
# Set default values for any applicable custom fields
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
for component in components:
component.custom_field_data = cf_defaults
model.objects.bulk_create(components)
# Manually send the post_save signal for each of the newly created components
for component in components:
Expand All @@ -1019,7 +1018,11 @@ def _instantiate_components(self, queryset, bulk_create=True):
update_fields=None
)
else:
for component in components:
for obj in queryset:
component = obj.instantiate(device=self)
# Set default values for any applicable custom fields
if cf_defaults := CustomField.objects.get_defaults_for_model(model):
component.custom_field_data = cf_defaults
component.save()

def save(self, *args, **kwargs):
Expand Down

0 comments on commit 3b3511c

Please sign in to comment.