mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Add choices ArrayField to CustomField; drop CustomFieldChoice
This commit is contained in:
35
netbox/extras/migrations/0050_customfield_add_choices.py
Normal file
35
netbox/extras/migrations/0050_customfield_add_choices.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import django.contrib.postgres.fields
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('extras', '0049_remove_graph'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# Rename reverse relation on CustomFieldChoice
|
||||
migrations.AlterField(
|
||||
model_name='customfieldchoice',
|
||||
name='field',
|
||||
field=models.ForeignKey(
|
||||
limit_choices_to={'type': 'select'},
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name='_choices',
|
||||
to='extras.customfield'
|
||||
),
|
||||
),
|
||||
# Add choices field to CustomField
|
||||
migrations.AddField(
|
||||
model_name='customfield',
|
||||
name='choices',
|
||||
field=django.contrib.postgres.fields.ArrayField(
|
||||
base_field=models.CharField(max_length=100),
|
||||
blank=True,
|
||||
null=True,
|
||||
size=None
|
||||
),
|
||||
),
|
||||
]
|
@@ -3,18 +3,38 @@ from django.db import migrations
|
||||
from extras.choices import CustomFieldTypeChoices
|
||||
|
||||
|
||||
def deserialize_value(field_type, value):
|
||||
def deserialize_value(field, value):
|
||||
"""
|
||||
Convert serialized values to JSON equivalents.
|
||||
"""
|
||||
if field_type in (CustomFieldTypeChoices.TYPE_INTEGER, CustomFieldTypeChoices.TYPE_SELECT):
|
||||
if field.type in (CustomFieldTypeChoices.TYPE_INTEGER):
|
||||
return int(value)
|
||||
if field_type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
||||
if field.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
|
||||
return bool(int(value))
|
||||
if field.type == CustomFieldTypeChoices.TYPE_SELECT:
|
||||
return field._choices.get(pk=int(value)).value
|
||||
return value
|
||||
|
||||
|
||||
def migrate_customfieldchoices(apps, schema_editor):
|
||||
"""
|
||||
Collect all CustomFieldChoices for each applicable CustomField, and save them locally as an array on
|
||||
the CustomField instance.
|
||||
"""
|
||||
CustomField = apps.get_model('extras', 'CustomField')
|
||||
CustomFieldChoice = apps.get_model('extras', 'CustomFieldChoice')
|
||||
|
||||
for cf in CustomField.objects.filter(type='select'):
|
||||
cf.choices = [
|
||||
cfc.value for cfc in CustomFieldChoice.objects.filter(field=cf).order_by('weight', 'value')
|
||||
]
|
||||
cf.save()
|
||||
|
||||
|
||||
def migrate_customfieldvalues(apps, schema_editor):
|
||||
"""
|
||||
Copy data from CustomFieldValues into the custom_field_data JSON field on each model instance.
|
||||
"""
|
||||
CustomFieldValue = apps.get_model('extras', 'CustomFieldValue')
|
||||
|
||||
for cfv in CustomFieldValue.objects.prefetch_related('field').exclude(serialized_value=''):
|
||||
@@ -24,7 +44,7 @@ def migrate_customfieldvalues(apps, schema_editor):
|
||||
# TODO: This can be done more efficiently once .update() is supported for JSON fields
|
||||
cf_data = model.objects.filter(pk=cfv.obj_id).values('custom_field_data').first()
|
||||
try:
|
||||
cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field.type, cfv.serialized_value)
|
||||
cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field, cfv.serialized_value)
|
||||
except ValueError as e:
|
||||
print(f'{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})')
|
||||
raise e
|
||||
@@ -36,7 +56,7 @@ class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('circuits', '0020_custom_field_data'),
|
||||
('dcim', '0115_custom_field_data'),
|
||||
('extras', '0049_remove_graph'),
|
||||
('extras', '0050_customfield_add_choices'),
|
||||
('ipam', '0038_custom_field_data'),
|
||||
('secrets', '0010_custom_field_data'),
|
||||
('tenancy', '0010_custom_field_data'),
|
||||
@@ -44,6 +64,9 @@ class Migration(migrations.Migration):
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
code=migrate_customfieldchoices
|
||||
),
|
||||
migrations.RunPython(
|
||||
code=migrate_customfieldvalues
|
||||
),
|
@@ -1,15 +1,16 @@
|
||||
# Generated by Django 3.1 on 2020-08-21 19:52
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('extras', '0050_migrate_customfieldvalues'),
|
||||
('extras', '0051_migrate_customfields'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.DeleteModel(
|
||||
name='CustomFieldChoice',
|
||||
),
|
||||
migrations.DeleteModel(
|
||||
name='CustomFieldValue',
|
||||
),
|
Reference in New Issue
Block a user