0

I use a queryset fields in 2 places on my view.

One is ordered by another field, the other is ordered by facet. I'd like to order the dataset in 2 ways on the page, rather than passing the queryset twice.

I am trying to use dictsort to do this as is outlined here and a few other questions.

I have this in my code:

{% for field in fields|dictsort:"facet_order" %}
    field.facet_order
{% endfor %}

But i see nothing rendered.

The loop works fine without the dictsort, and as a sanity check i have run the below with no problem:

{% for field in fields %}
    field.facet_order
{% endfor %}

It yields:

None None None None 1 0 None ...

My code looks to me like it strongly resembles django's documentation example below:

{% for book in books|dictsort:"author.age" %}
    * {{ book.title }} ({{ book.author.name }})
{% endfor %}

What am i doing wrong here?

Preston
  • 7,399
  • 8
  • 54
  • 84

1 Answers1

0

To answer my own question, i had to register a custom template function as outlined in the answer i found here

I did this by following djangos example outlined here:

# Add templatetags/poll_extras.py
polls/
    templatetags/
        __init__.py
        poll_extras.py


# Inside templatetags/poll_extras.py
from django import template
register = template.Library()

@register.filter
def sort_by(queryset, order):
    return queryset.order_by(order)


# in the template:
{% load poll_extras %}
Preston
  • 7,399
  • 8
  • 54
  • 84