I have following django query set where I need to sort the output of the one of the component I am trying to filter it out using following code but it generate the dict output
{%- for i in queryset %}
{% set i1 = i.rack.name %}
{% set i2 = i1 | sort( reverse=false, attribute=i1) %}
{{i2}},{{i.name}}
{%- endfor %}
Thanks @petro
queryset is the django queryset, currently this script used in netbox
so if i use the i.name then it will give you hostaname same way if i use āi.serialā then it will give me serial number of the device same way āi.rack.nameā give me rack number so in above output i want to sort the data with rack name (A20,C19,E14) like wiseā¦
Thanks @petro
Let me try to make it simple (if i can )
in simple term queryset is collection of data from the databse like [x1:C20 , x2:A19 , x3:E14)
for loop query the x1,x2 and x3 but I want to sort the output in
x2:A19,x1:C20,x3:E14
The data you are faking out is not possible. Please either past the actual data (removing what you donāt want to show here) or replicate the data properly.
[ ] indicates itās a list, however x1: C20 indicates itās a dictionary, but the key or value are not strings but contain string-like characters. Making the whole thing just not possible.
I have following netbox export template code to sort the data with rack name what is the issue with the code {%- for i in queryset %} {% set i1 = i.rack.name %} {% set i2 = i1 | sort( reverse=false, attribute=i1) %} {{i2}},{{i.name}} {%- endfor %}
The issue with your code is that the sort filter youāre using isnāt working as expected. Hereās the problem and how to fix it:
Sorting within the template: Templates in Netbox are primarily for displaying data, not manipulating it. The sort filter is intended for sorting within the template itself (e.g., sorting displayed names).
Sorting the queryset: To sort the data by rack name before displaying it, you need to modify the queryset itself. This can be achieved using Djangoās built-in order_by method.
Hereās the corrected code:
**Django**
{% for device in queryset.order_by("rack__name") %}
{{ device.rack.name }}, {{ device.name }}
{% endfor %}