mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 02:48:38 -06:00
Optimize rename_object_data()
This commit is contained in:
parent
48c9cc6182
commit
a6384e6c9c
@ -290,7 +290,12 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
|||||||
value = Value(self.default, models.JSONField())
|
value = Value(self.default, models.JSONField())
|
||||||
for ct in content_types:
|
for ct in content_types:
|
||||||
ct.model_class().objects.update(
|
ct.model_class().objects.update(
|
||||||
custom_field_data=Func(F('custom_field_data'), Value([self.name]), value, function='jsonb_set')
|
custom_field_data=Func(
|
||||||
|
F('custom_field_data'),
|
||||||
|
Value([self.name]),
|
||||||
|
value,
|
||||||
|
function='jsonb_set'
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
def remove_stale_data(self, content_types):
|
def remove_stale_data(self, content_types):
|
||||||
@ -305,15 +310,21 @@ class CustomField(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel):
|
|||||||
|
|
||||||
def rename_object_data(self, old_name, new_name):
|
def rename_object_data(self, old_name, new_name):
|
||||||
"""
|
"""
|
||||||
Called when a CustomField has been renamed. Updates all assigned object data.
|
Called when a CustomField has been renamed. Removes the original key and inserts the new
|
||||||
|
one, copying the value of the old key.
|
||||||
"""
|
"""
|
||||||
for ct in self.object_types.all():
|
for ct in self.object_types.all():
|
||||||
model = ct.model_class()
|
ct.model_class().objects.update(
|
||||||
params = {f'custom_field_data__{old_name}__isnull': False}
|
custom_field_data=Func(
|
||||||
instances = model.objects.filter(**params)
|
F('custom_field_data') - old_name,
|
||||||
for instance in instances:
|
Value([new_name]),
|
||||||
instance.custom_field_data[new_name] = instance.custom_field_data.pop(old_name)
|
Func(
|
||||||
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
|
F('custom_field_data'),
|
||||||
|
function='jsonb_extract_path_text',
|
||||||
|
template=f"to_jsonb(%(expressions)s -> '{old_name}')"
|
||||||
|
),
|
||||||
|
function='jsonb_set')
|
||||||
|
)
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
Loading…
Reference in New Issue
Block a user