Skip to content

Instantly share code, notes, and snippets.

@cimnine
Last active June 20, 2024 03:37
Show Gist options
  • Save cimnine/f3591d26ecc41111563ff26741669c6c to your computer and use it in GitHub Desktop.
Save cimnine/f3591d26ecc41111563ff26741669c6c to your computer and use it in GitHub Desktop.
A script to import custom fields from a YAML into Netbox

custom field import script

Usage:

./manage.py shell --plain < import_custom_fields.py

A Note On Boolean Custom Fields

When you want to set the default value of a type: boolean custom field to false, you need to wrap the value false in quotes, like so:

boolean_custom_field:
  type: boolean
  default: 'False'
some_text_field:
type: text
label: blablabla
description: blablabla
required: false
filterable: true
default: This is the default.
weight: 0
on_objects:
- dcim.models.Device
- dcim.models.Rack
- ipam.models.IPAddress
- ipam.models.Prefix
- tenancy.models.Tenant
- virtualization.models.VirtualMachine
some_integer_value:
type: integer
label: blablabla
description: blablabla
required: true
filterable: true
weight: 10
on_objects:
- tenancy.models.Tenant
a_selection_field: &field_ref
type: selection
label: blablabla
required: false
filterable: true
weight: 30
on_objects:
- dcim.models.Device
choices:
- value: blablabla
weight: 10
- value: blablablablablabla
weight: 20
- value: blablablablablablablablabla
weight: 30
another_selection_with_same_choices:
<<: *field_ref
label: blablabla222
weight: 40
from extras.constants import CF_TYPE_TEXT, CF_TYPE_INTEGER, CF_TYPE_BOOLEAN, CF_TYPE_DATE, CF_TYPE_URL, CF_TYPE_SELECT
from extras.models import CustomField, CustomFieldChoice
from ruamel.yaml import YAML
text_to_fields = {
'boolean': CF_TYPE_BOOLEAN,
'date': CF_TYPE_DATE,
'integer': CF_TYPE_INTEGER,
'selection': CF_TYPE_SELECT,
'text': CF_TYPE_TEXT,
'url': CF_TYPE_URL,
}
def get_class_for_class_path(class_path):
import importlib
from django.contrib.contenttypes.models import ContentType
module_name, class_name = class_path.rsplit(".", 1)
module = importlib.import_module(module_name)
clazz = getattr(module, class_name)
return ContentType.objects.get_for_model(clazz)
with open('custom_fields.yml', 'r') as stream:
yaml = YAML(typ='safe')
customfields = yaml.load(stream)
for cf_name, cf_details in customfields.items():
custom_field, created = CustomField.objects.get_or_create(name = cf_name)
if created:
if cf_details.get('default', 0):
custom_field.default = cf_details['default']
if cf_details.get('description', 0):
custom_field.description = cf_details['description']
if cf_details.get('filterable', 0):
custom_field.is_filterables = cf_details['filterable']
if cf_details.get('label', 0):
custom_field.label = cf_details['label']
for object_type in cf_details.get('on_objects', []):
custom_field.obj_type.add(get_class_for_class_path(object_type))
if cf_details.get('required', 0):
custom_field.required = cf_details['required']
if cf_details.get('type', 0):
custom_field.type = text_to_fields[cf_details['type']]
if cf_details.get('weight', 0):
custom_field.weight = cf_details['weight']
custom_field.save()
for choice_details in cf_details.get('choices', []):
choice = CustomFieldChoice.objects.create(
field=custom_field,
value=choice_details['value'])
if choice_details.get('weight', 0):
choice.weight = choice_details['weight']
choice.save()
print("🔧 Created custom field", cf_name)

Copyright 2018 Nine Internet Solutions AG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@punkdata
Copy link

Thank you for this file. I was having a bit of trouble generating the yaml for CFs but this gist straightened me out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment