Fix custom fields initializer

This commit is contained in:
Christian Mäder 2020-12-14 22:09:08 +01:00
parent 234baa40a5
commit e383fd42bd
3 changed files with 14 additions and 12 deletions

View File

@ -52,7 +52,7 @@
# - Third Item
# - Fifth Item
# - Fourth Item
# select_field_auto_weight:
# select_field_legacy_format:
# type: select
# label: Choose between items
# required: false
@ -61,11 +61,12 @@
# on_objects:
# - dcim.models.Device
# choices:
# - A
# - B
# - C
# - E
# - D like deprecated
# - value: A # this is the deprecated format.
# - value: B # we only use it for the tests.
# - value: C # please see above for the new format.
# - value: "D like deprecated"
# weight: 999
# - value: E
# boolean_field:
# type: boolean
# label: Yes Or No?

View File

@ -1,6 +1,6 @@
import sys
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import User
from startup_script_utils import load_yaml, set_permissions
from users.models import Token

View File

@ -45,12 +45,13 @@ for cf_name, cf_details in customfields.items():
if cf_details.get('choices', False):
custom_field.choices = []
for choice_detail in enumerate(cf_details.get('choices', [])):
if isinstance(choice_detail, str):
custom_field.choices.append(choice_detail)
else: # legacy mode
print(f"⚠️ Please migrate the 'choices' of '{cf_name}' to the new format, as 'weight' is no longer supported!")
for choice_detail in cf_details.get('choices', []):
if isinstance(choice_detail, dict) and 'value' in choice_detail:
# legacy mode
print(f"⚠️ Please migrate the choice '{choice_detail['value']}' of '{cf_name}' to the new format, as 'weight' is no longer supported!")
custom_field.choices.append(choice_detail['value'])
else:
custom_field.choices.append(choice_detail)
custom_field.save()