diff --git a/initializers/custom_fields.yml b/initializers/custom_fields.yml index ccde9a0..71c8b85 100644 --- a/initializers/custom_fields.yml +++ b/initializers/custom_fields.yml @@ -40,6 +40,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 5c40e37..3492baf 100644 --- a/startup_scripts/20_custom_fields.py +++ b/startup_scripts/20_custom_fields.py @@ -56,13 +56,14 @@ 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, created = 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'], + ) + # Add weight after initial creation to fix a bug if you use the same 'value' + # for multiple custom fields. + choice.weight = choice_details.get('weight', idx * 10) + choice.save() print("🔧 Created custom field", cf_name)