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

View File

@ -1,6 +1,6 @@
import sys 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 startup_script_utils import load_yaml, set_permissions
from users.models import Token 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): if cf_details.get('choices', False):
custom_field.choices = [] custom_field.choices = []
for choice_detail in enumerate(cf_details.get('choices', [])): for choice_detail in cf_details.get('choices', []):
if isinstance(choice_detail, str): if isinstance(choice_detail, dict) and 'value' in choice_detail:
custom_field.choices.append(choice_detail) # legacy mode
else: # legacy mode print(f"⚠️ Please migrate the choice '{choice_detail['value']}' of '{cf_name}' to the new format, as 'weight' is no longer supported!")
print(f"⚠️ Please migrate the 'choices' of '{cf_name}' to the new format, as 'weight' is no longer supported!")
custom_field.choices.append(choice_detail['value']) custom_field.choices.append(choice_detail['value'])
else:
custom_field.choices.append(choice_detail)
custom_field.save() custom_field.save()