def attach_private_projects_same_owner(queryset, user, as_field='private_projects_same_owner_attr'):
 model = queryset.model
 if ((user is None) or user.is_anonymous()):
  sql = 'SELECT 0'
 else:
  sql = '\n                 SELECT COUNT(id)\n                   FROM projects_project p_aux\n                  WHERE p_aux.is_private = True AND\n                        p_aux.owner_id = {tbl}.owner_id\n              '
  sql = sql.format(tbl=model._meta.db_table, user_id=user.id)
 queryset = queryset.extra(select={as_field: sql})
 return queryset

def attach_private_projects_same_owner ( queryset , user , as_field = 'private_projects_same_owner_attr' ) : model = queryset . model if ( ( user is None ) or user . is_anonymous ( ) ) : sql = 'SELECT 0' else : sql = '\n                 SELECT COUNT(id)\n                   FROM projects_project p_aux\n                  WHERE p_aux.is_private = True AND\n                        p_aux.owner_id = {tbl}.owner_id\n              ' sql = sql . format ( tbl = model . _meta . db_table , user_id = user . id ) queryset = queryset . extra ( select = { as_field : sql } ) return queryset
attach a private projects counter to each object of the queryset .