Reputation: 9643
I have a custom filter that joins an array of items by a specified character. For example it will turn:
In view:
test_array = ['one', 'two', 'three', 'four']
In template:
{{ test_array |joinby:"," }}
Output:
one, two, three, four
I need to apply this same filter to a queryset of related items from a model. I have tried this so far:
{{ user.related_model.all.name|joinby:"," }}
However it just returns nothing. What is the best way to get this type of query to work?
Upvotes: 0
Views: 90
Reputation: 99650
You might want to keep the template clean, and create a filter, or handle this in your view.
In the view:
def my_view(request):
#blah
user_followers = request.user.related_model.values_list('name', flat=True)
and in the template
{{ user_followers | joinby: "," }}
Or
Register a filter
@register.filter(name='related_names')
def get_related_names(user):
user_followers = user.related_model.values_list('name', flat=True)
return ", ".join(user_followers)
and in the template:
{% related_names %}
Upvotes: 1