Export Template Containing Values From Objects Not In Queryset #17115
Replies: 1 comment 3 replies
-
I can't see how your code ever worked with Netbox 2.x, since the array of terminations wasn't added until 3.3
I don't understand which object you're talking about, or even what the Use nbshell to try this out interactively:
Remember of course that terminations is a list: if there are multiple terminations, either you just pick the first one (as above), or you must iterate over them all.
What do you mean? Which model are you talking about?
I don't really understand any of that, except that you can start a new query within a template by using the top-level model classes passed in, such as
Example:
You're right that it's not obvious how the REST API generates the Therefore, I think you render the cable (or device etc) as a string:
This will happen by default anyway if you render the object in a template, e.g. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to adjust aIs performing a subquery even supported and, if so, how would I go about doing so?DCIM\Cable
export template I had in place for the old (pre 3.x) cable model. Specifically, I'm having issue getting the serial of the devices of[ab]_terminations
. The old code was simply{% if cable.b_terminations.object.device.serial %}({{cable.b_terminations.object.device.serial[-4:]}}){% endif %}
to add the last 4 digits of the serial number wrapped in parenthesis(xxxx)
. However, since the new model does not include the serial in the object itself anymore, I need to make a fresh query to thedevice
to access that field. It should be trivial to get the output I want after having access to that data, but getting the data in the first place is the hard part. The pkid of the device is already in the data as should be the device URL, though I can't actually get that to print in the template for some reason (string wrapping of characters or escaping the whole string possibly? I'm not familiar enough with Jinja2 to bother spending time on it). I just can't figure out how to query thatdevice
object itself within the template code.EDIT:Looking back on the original template, I'll need to include the interface label from the terminations as well. Oddly, the cable already has the
display
property (at least according to the API) which is exactly the value I want, but - like the URL - I can't get that into the template for some reasonA bad case of the Fridays invalidated my initial use case, as @candlerb helped determine. However, the root question seems to be open still - Is performing a subquery within an export template supported, and how would one go about doing so?
Using a cable's attached device serial as an example (even though it's easily accessible directly). Even ruling out attempting to use a variable for the device PKID, any cased combination of
dcim.device.objects.get(pk=n).serial
or just straight updevice.objects.get(pk=n).serial
always complains about the master object (either DCIM or Device in that casing or all lower) being undefined, with or withoutDCIM | Device
in the assigned models, if the original object type to be exported is not a Device (in this case, a Cable). According to nbshellDevice.objects.get(pk=n).serial
should be all that's necessary, but there appears to be something within how the Export request is associated with another model initially that prevents it. It appears that functionality was provided for at least Config Templates in #12814 but does not seem functional within Export Templates.Beta Was this translation helpful? Give feedback.
All reactions