# Buggy code
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=""
    ):
        model = apps.get_model(cfv.obj_type.app_label, cfv.obj_type.model)

        # Read and update custom field value for each instance
        # 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, cfv.serialized_value
            )
        except ValueError as e:
            print(
                f"{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})"
            )
            raise e
        model.objects.filter(pk=cfv.obj_id).update(**cf_data)


# Traceback (TypeError)
Applying extras.0051_migrate_customfields...Traceback (most recent call last):
File "netbox/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 245, in handle
fake_initial=fake_initial,
File "/opt/netbox/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
state = migration.apply(state, schema_editor)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/opt/netbox/venv/lib/python3.6/site-packages/django/db/migrations/operations/special.py", line 190, in database_forwards
self.code(from_state.apps, schema_editor)
File "/opt/netbox/netbox/extras/migrations/0051_migrate_customfields.py", line 69, in migrate_customfieldvalues
cf_data['custom_field_data'][cfv.field.name] = deserialize_value(cfv.field, cfv.serialized_value)
TypeError: 'NoneType' object is not subscriptable

# Fixed code
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=""
    ):
        model = apps.get_model(cfv.obj_type.app_label, cfv.obj_type.model)

        # Read and update custom field value for each instance
        # 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, cfv.serialized_value
            )
        except Exception as e:
            print(
                f"{cfv.field.name} ({cfv.field.type}): {cfv.serialized_value} ({cfv.pk})"
            )
            raise e
        model.objects.filter(pk=cfv.obj_id).update(**cf_data)