Skip to content

Commit 10847e2

Browse files
committed
Optimize addition/removal of default custom field values
1 parent 9b0258f commit 10847e2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

netbox/extras/models/customfields.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ def populate_initial_data(self, content_types):
127127
"""
128128
for ct in content_types:
129129
model = ct.model_class()
130-
for obj in model.objects.exclude(**{f'custom_field_data__contains': self.name}):
131-
obj.custom_field_data[self.name] = self.default
132-
obj.save()
130+
instances = model.objects.exclude(**{f'custom_field_data__contains': self.name})
131+
for instance in instances:
132+
instance.custom_field_data[self.name] = self.default
133+
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
133134

134135
def remove_stale_data(self, content_types):
135136
"""
@@ -138,9 +139,10 @@ def remove_stale_data(self, content_types):
138139
"""
139140
for ct in content_types:
140141
model = ct.model_class()
141-
for obj in model.objects.filter(**{f'custom_field_data__{self.name}__isnull': False}):
142-
del(obj.custom_field_data[self.name])
143-
obj.save()
142+
instances = model.objects.filter(**{f'custom_field_data__{self.name}__isnull': False})
143+
for instance in instances:
144+
del(instance.custom_field_data[self.name])
145+
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
144146

145147
def rename_object_data(self, old_name, new_name):
146148
"""

0 commit comments

Comments
 (0)