[Answered ]-Python django how can i filter object_set.all by current user in template

2👍

One way to do this in the template would be to define a custom filter. This custom filter can accept a queryset and the currently logged in user as arguments and do the necessary filtering.

@register.filter
def filter_by_user(queryset, user):
    """Filter the queryset by (currently logged in) user"""
    return queryset.filter(added_by = user)

And in the template:

<td>{{ book.rating_set.all|filter_by_user:user|safeseq|join:", " }}</td>

Leave a comment