[Answer]-How to get a list of related items and apply a template filter in Django

1👍

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

Leave a comment