Skip to content

Custom Script to Add Device to Existing or New Tenant #17905

Discussion options

You must be logged in to vote

The function you should use in your custom NetBox script is get_or_create(). It returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.

Hope this helps!

Example:

https://github.com/netbox-community/netbox/blob/4ca2b21a706f86fdb4ff5bf1940960f73ed74530/netbox/utilities/testing/utils.py#L47C1-L50C107

    manufacturer, _ = Manufacturer.objects.get_or_create(name='Manufacturer 1', slug='manufacturer-1')
    devicetype, _ = DeviceType.objects.get_or_create(model='Device Type 1', manufacturer=manufacturer)
    devicerole, _ = DeviceRole.objects.get_or_create(name='Device Role 1', slug='device-role…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@paulcarre07
Comment options

Answer selected by paulcarre07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants