You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I am trying to access some of the data we have configured on a site in netbox. All sites have unique SiteID. But I am not able to get access to this information from my configuration template.
I have the following Jinja2 code where the first line does not output anything but the for-loop shows the Names of all the Custom Fields I have configred on the site, where this device is located.
{{ device.site.custom_fields.site_id }}
{% for field in device.site.custom_fields.all() %} field: {{field}} {%- endfor %}
Any suggestions on how to access this data would be appreciated.
Try {{ device.site.cf.site_id }}. If that doesn't work then {{ device.site.cf["site_id"] }} or {{ device.site.custom_field_data["site_id"] }}
custom_field_data contains the raw {attr: value} dict of custom field values for this particular object (e.g. an individual Device) stored in the database, whereas custom_fields is a QuerySet of custom field definitions for this model (e.g. the Device class)
cf is slightly different because it takes custom_field_data and deserializes it, which in some cases is different (e.g. you get an Object representation for an Object custom field, instead of the object ID)
@cached_property
def cf(self):
"""
Return a dictionary mapping each custom field for this instance to its deserialized value.
```python
>>> tenant = Tenant.objects.first()
>>> tenant.cf
{'primary_site': <Site: DM-NYC>, 'cust_id': 'DMI01', 'is_active': True}
```
"""
return {
cf.name: cf.deserialize(self.custom_field_data.get(cf.name))
for cf in self.custom_fields
}
Aside: note that site_id isn't a great name for a custom field, since it overlaps with lots of existing Netbox attributes. In particular device.site_id is the core attribute which links a device to its enclosing site; the ID in this case is the sequential database ID. So it could be confusing between device.site_id and device.site.id, both of which are a database sequential ID, and device.site.cf.site_id which is your custom field on Site. (But it will work fine because cf distinguishes it)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I am trying to access some of the data we have configured on a site in netbox. All sites have unique SiteID. But I am not able to get access to this information from my configuration template.
I have the following Jinja2 code where the first line does not output anything but the for-loop shows the Names of all the Custom Fields I have configred on the site, where this device is located.
Any suggestions on how to access this data would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions