#!/usr/bin/env python3 import pynetbox,json nb = pynetbox.api( 'https://demo.netbox.dev', token='99f8a8c0a8fdda98c7fb41ac65f38c7a031818ba') mfn='mysamplemanufacturer' mf = nb.dcim.manufacturers.get(name=mfn) if not mf: mf = nb.dcim.manufacturers.create(name=mfn, slug=mfn) dtn='mysamplemodel' dt = nb.dcim.device_types.get(model=dtn) if not dt: dt = nb.dcim.device_types.create(model=dtn, slug=dtn, manufacturer=mf.id) drn='mysamplerole' dr = nb.dcim.device_roles.get(name=drn) if not dr: dr = nb.dcim.device_roles.create(name=drn, slug=drn) dsn='mysamplesite' ds = nb.dcim.sites.get(name=dsn) if not ds: ds = nb.dcim.sites.create(name=dsn, slug=dsn) tmpldev = dict(device_type=dt.id,device_role=dr.id,site=ds.id) d1n = 'mysampledevice1' d1 = nb.dcim.devices.get(name=d1n) if not d1: d1 = nb.dcim.devices.create(name=d1n, **tmpldev) d2n = 'mysampledevice2' d2 = nb.dcim.devices.get(name=d1n) if not d2: d2 = nb.dcim.devices.create(name=d2n, **tmpldev) cfn = 'mycustomfield' cf = nb.extras.custom_fields.get(name=cfn) if not cf: cf = nb.extras.custom_fields.create(name=cfn, content_types=['dcim.interface'], type='object', object_type='dcim.interface') ifn = 'mysampleinterface' i1 = nb.dcim.interfaces.get(device=d1,name=ifn) if not i1: i1 = nb.dcim.interfaces.create(device=d1.id, name=ifn, type='other') i2 = nb.dcim.interfaces.get(device=d2,name=ifn) if not i2: i2 = nb.dcim.interfaces.create(device=d1.id, name=ifn, type='other') i2.update(dict(custom_fields={cfn:i1.id})) i2.save() i2 = nb.dcim.interfaces.get(device=d2,name=ifn) print(f"Field populated: {i2.custom_fields}") print(f"Let's try to update label") i2.update(dict(label='test'))