diff --git a/initializers/custom_fields.yml b/initializers/custom_fields.yml index c502468..1409167 100644 --- a/initializers/custom_fields.yml +++ b/initializers/custom_fields.yml @@ -39,6 +39,21 @@ # weight: 50 # - value: Fourth Item # weight: 40 +# selection_field_auto_weight: +# type: selection +# label: Choose between items +# required: false +# filterable: true +# weight: 30 +# on_objects: +# - dcim.models.Device +# choices: +# - value: A +# - value: B +# - value: C +# - value: "D like deprecated" +# weight: 999 +# - value: E # boolean_field: # type: boolean # label: Yes Or No? diff --git a/startup_scripts/20_custom_fields.py b/startup_scripts/20_custom_fields.py index 5b8284e..1346869 100644 --- a/startup_scripts/20_custom_fields.py +++ b/startup_scripts/20_custom_fields.py @@ -59,13 +59,11 @@ with open('/opt/netbox/initializers/custom_fields.yml', 'r') as stream: custom_field.save() - for choice_details in cf_details.get('choices', []): - choice = CustomFieldChoice.objects.create( + for idx, choice_details in enumerate(cf_details.get('choices', [])): + choice, _ = CustomFieldChoice.objects.get_or_create( field=custom_field, - value=choice_details['value']) - - if choice_details.get('weight', 0): - choice.weight = choice_details['weight'] - choice.save() + value=choice_details['value'], + defaults={'weight': idx * 10} + ) print("🔧 Created custom field", cf_name)