Cleaner way to get tags in device export

765 views
Skip to first unread message

Brian Candler

unread,
Jun 2, 2019, 3:22:21 PM6/2/19
to NetBox
I am trying to export a list of devices with their tags (since the device listing doesn't show the tags)

The taggit API includes a names() method.  Here's the export template I'm trying to use:

{% for device in queryset %}
{{ device.name }},{{ device.site }},{{ device.device_role }},{{ device.status }},{{ device.tags.names }}
{% endfor %}


The results with Jinja2 query language aren't useful, since it doesn't invoke the method:

storage1,WRN,Server,1,<bound method _TaggableManager.names of <taggit.managers._TaggableManager object at 0x7f80882c1668>>

If I use the Django template language then the method is invoked, but the results are ugly:

storage1,WRN,Server,1,&lt;QuerySet [&#39;4.1U&#39;, &#39;move1&#39;]&gt;

(there the tags should be "4.1U" and "move1")

I tried adding 'join'

{% for device in queryset %}
{{ device.name }},{{ device.site }},{{ device.device_role }},{{ device.status }},{{ device.tags.names | join: ";" }}
{% endfor %}

This gives an error saying that join requires 2 arguments, only 1 given.

I finally ended up with this:

{% for device in queryset %}
{{ device.name }},{{ device.site }},{{ device.device_role }},{{ device.status }},{% for tag in device.tags.names %}{{tag}}{% if not forloop.last %};{% endif %}{%endfor %}{% endfor %}

It works, but is there a better way?

Jeremy Stretch

unread,
Jun 3, 2019, 10:12:00 AM6/3/19
to Brian Candler, NetBox
I haven't tried this, but is the issue simply that you need to invoke the method with the parentheses, like this?

{{ device.tags.names() | join: ";" }}

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netbox-discuss/e67683f4-1300-4614-9274-956663b76f2d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Candler

unread,
Jun 3, 2019, 5:34:23 PM6/3/19
to NetBox
On Monday, 3 June 2019 15:12:00 UTC+1, Jeremy Stretch wrote:
I haven't tried this, but is the issue simply that you need to invoke the method with the parentheses, like this?

{{ device.tags.names() | join: ";" }}


I presume you mean with Django templating language rather than Jinja2 (because of the colon after join).  I've never seen the () syntax with this, because Django templating language invokes methods with no arguments automatically.

Anyway, testing the following export template

{% for device in queryset %}
{{ device.name }},{{ device.tags.names() | join: ";" }}{% endfor %}

With Django:

There was an error rendering the selected export template (test): Could not parse some characters: device.tags.names|()| | join: ";"

With Jinja2:

There was an error rendering the selected export template (test): expected token 'end of print statement', got ':'

Jeremy Stretch

unread,
Jun 4, 2019, 9:35:22 AM6/4/19
to Brian Candler, NetBox
I was just pointing out that you need to add the parentheses if using Jinja2: The Django template language will try calling it as a method without the parentheses but Jinja2 won't.

Django example:

{% for device in queryset %}
{{ device.name }}: {{ device.tags.names | join:", " }}
{% endfor %}

JInja2 example:

{% for device in queryset %}
{{ device.name }}: {{ device.tags.names() | join(", ") }}
{% endfor %}

--
You received this message because you are subscribed to the Google Groups "NetBox" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netbox-discus...@googlegroups.com.
To post to this group, send email to netbox-...@googlegroups.com.

Brian Candler

unread,
Jun 4, 2019, 10:20:59 AM6/4/19
to NetBox
On Tuesday, 4 June 2019 14:35:22 UTC+1, Jeremy Stretch wrote:

{% for device in queryset %}
{{ device.name }}: {{ device.tags.names() | join(", ") }}
{% endfor %}


That works - thank you!

I wasn't aware that you could call a callable from Jinja2, but I see it in the documentation now.
Reply all
Reply to author
Forward
0 new messages