Jinja2 output sorting issue

Hi,

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 %}

Current output

['E', '1', '4'],x3
['C', '2', '0'],x1
['A', '1', '9'],x2

Expected output

A19,x2
C20,x1
E14,x3

How to fix this issue?
Thanks in Advance :slight_smile:

{{ i2 | join('') }}

Thatā€™s assuming your

{% set i2 = i1 | sort( reverse=false, attribute=i1) %}

line is correct.

Which, to be honest, does not look correct.

Thereā€™s a good chance that this is really what youā€™re attempting to doā€¦

{%- for i in queryset | sort(attribute='rack.name') %}
{{ i.rack.name }}, {{ i.name }}
{% endfor %}

But I canā€™t tell without knowing what queryset actually returns.

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ā€¦

Did you try the template?

Yes , it is not working becuase it cannot find the attribute ā€˜nameā€™

Then you have to post your data in queryset, otherwise itā€™s a guessing game.

Thanks @petro
Let me try to make it simple (if i can :slight_smile: )
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.

Thanks for your time

Issue has been fixed with following code (Gemini AI helped me to get correct code )

{% for device in queryset.order_by("rack__name") %}
  {{ device.rack.name }}, {{ device.name }}
{% endfor %}

Please post the question you asked Gemini AI and its answer.

I have asked following question to gemini

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 %}

here is the answer from the gemini

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:

  1. 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).
  2. 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 %}