@register.filter(name='user_which_groups')
def user_which_group(user, member):
 member = getattr(user, member)
 names = [members.name for members in member.all()]
 return ','.join(names)

@ register . filter ( name = 'user_which_groups' ) def user_which_group ( user , member ) : member = getattr ( user , member ) names = [ members . name for members in member . all ( ) ] return ',' . join ( names )
instance is a user object .