#!/opt/netbox/venv/bin/python import django import os import sys sys.path.append('/opt/netbox/netbox') os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'netbox.settings') django.setup() from dcim.models import Device, ConsolePort, ConsoleServerPort, PowerPort, PowerOutlet, Interface, RearPort, FrontPort, DeviceBay from django.db import transaction transaction.set_autocommit(False) for device in Device.objects.all(): # Based on Device.save() ConsolePort.objects.bulk_create( [ConsolePort(device=device, name=template.name) for template in device.device_type.consoleporttemplates.all() if template.name not in {i.name for i in device.consoleports.all()}] ) ConsoleServerPort.objects.bulk_create( [ConsoleServerPort(device=device, name=template.name) for template in device.device_type.consoleserverporttemplates.all() if template.name not in {i.name for i in device.consoleserverports.all()}] ) PowerPort.objects.bulk_create( [PowerPort(device=device, name=template.name) for template in device.device_type.powerporttemplates.all() if template.name not in {i.name for i in device.powerports.all()}] ) PowerOutlet.objects.bulk_create( [PowerOutlet(device=device, name=template.name) for template in device.device_type.poweroutlettemplates.all() if template.name not in {i.name for i in device.poweroutlets.all()}] ) Interface.objects.bulk_create( [Interface(device=device, name=template.name, type=template.type, mgmt_only=template.mgmt_only) for template in device.device_type.interfacetemplates.all() if template.name not in {i.name for i in device.interfaces.all()}] ) RearPort.objects.bulk_create([ RearPort( device=device, name=template.name, type=template.type, positions=template.positions ) for template in device.device_type.rearporttemplates.all() if template.name not in {i.name for i in device.rearports.all()} ]) FrontPort.objects.bulk_create([ FrontPort( device=device, name=template.name, type=template.type, rear_port=RearPort.objects.get(device=device, name=template.rear_port.name), rear_port_position=template.rear_port_position, ) for template in device.device_type.frontporttemplates.all() if template.name not in {i.name for i in device.frontports.all()} ]) DeviceBay.objects.bulk_create( [DeviceBay(device=device, name=template.name) for template in device.device_type.devicebaytemplates.all() if template.name not in {i.name for i in device.devicebays.all()}] ) #transaction.rollback() transaction.commit()