Replies: 2 comments 9 replies
-
Do # create device
dev = Device(site=xxx, status='active'...)
dev.full_clean()
dev.save()
# create IP address
ipaddr = IPAddress(address=data["primary_ip"])
ipaddr.full_clean()
ipaddr.save()
# assign primary IP to device
iface = Interface.objects.get(device_id=dev.id, name='Ethernet1')
iface.ip_addresses.set([ipaddr])
ipaddr = IPAddress.objects.get(id=ipaddr.id)
dev.refresh_from_db()
dev.primary_ip4 = ipaddr
dev.full_clean()
dev.save() Netbox added a half-baked caching implementation of component counts recently. This means that whenever you save a device, a signal is executed afterwards for calculating the count, meaning that the device object will always have to be reloaded after save to make sure you are not overwriting any post-save changes. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a custom script to create devices. I noticed a problem where new devices created this way do not show any power ports, outlets, and interfaces in the GUI, although they do exist, and the API shows counts of 0, for example
/api/dcim/devices/8178/
shows:The script runs like this (simplified)
It turns out, I have to run
dev = Device.objects.get(id=dev.id)
after saving the device, and that fixes the issue.Beta Was this translation helpful? Give feedback.
All reactions