1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Closes #5400: Store custom field defaults as JSON values

This commit is contained in:
Jeremy Stretch
2020-12-01 16:05:23 -05:00
parent 0b57389af6
commit cc271aefe1
8 changed files with 58 additions and 17 deletions

View File

@ -16,6 +16,28 @@ def deserialize_value(field, value):
return value
def migrate_customfield_defaults(apps, schema_editor):
"""
Copy old serialized defaults to native JSON types.
"""
CustomField = apps.get_model('extras', 'CustomField')
for customfield in CustomField.objects.exclude(default=''):
try:
if customfield.type == CustomFieldTypeChoices.TYPE_INTEGER:
value = int(customfield.default)
elif customfield.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
value = customfield.default in ['true', 'yes', '1']
else:
value = customfield.default
except ValueError:
raise ValueError(
f'Invalid default value "{customfield.default}" found for {customfield.type} '
f'custom field {customfield.name}'
)
CustomField.objects.filter(pk=customfield.pk).update(default2=value)
def migrate_customfieldchoices(apps, schema_editor):
"""
Collect all CustomFieldChoices for each applicable CustomField, and save them locally as an array on
@ -73,6 +95,9 @@ class Migration(migrations.Migration):
]
operations = [
migrations.RunPython(
code=migrate_customfield_defaults
),
migrations.RunPython(
code=migrate_customfieldchoices
),