Export template including parent/child device relationship

233 views
Skip to first unread message

steven.t...@gmail.com

unread,
Jan 19, 2022, 10:56:23 AM1/19/22
to NetBox
Hi,
I have a hopefully easy question. When we export a device list from Netbox by default it includes rack and position.  However, I am trying to create a custom jinja2 template to export some fields including parent device name and device bay, but I cannot seem to get it to accept the parent_device attributes.  I am going by the values I find in when I view the API object.  Can someone shed some light into what I am doing wrong please?

{% for device in queryset %}{{ device.name }},{{ device.serial }},{{ device.device_type.manufacturer }},{{ device.device_type.model }},{{ device.site }},{{ device.rack }},{{ device.position }},{{ device.parent_device.name }} ,{{ device.parent_device.device_bay.device.name }}
{% endfor %}

Brian Candler

unread,
Jan 19, 2022, 12:12:07 PM1/19/22
to NetBox
nbshell is a good way to explore this:

root@netbox:~# cd /opt/netbox
root@netbox:/opt/netbox# . venv/bin/activate
(venv) root@netbox:/opt/netbox# python3 netbox/manage.py nbshell

>>> help(Device)
...
>>> help(DeviceBay)
...

>>> d = Device.objects.get(name="mod-lc-s18-r18")
>>> d
<Device: mod-lc-s18-r18>
>>> d.parent_bay
<DeviceBay: 3>

Hence: this device is in parent device "pp-s18-1" in bay "3".

steven.t...@gmail.com

unread,
Jan 19, 2022, 4:07:41 PM1/19/22
to NetBox
Perfect, exactly what I was looking for.  Thank you so much :)
Reply all
Reply to author
Forward
0 new messages