def attach_is_watcher_to_queryset(queryset, user, as_field='is_watcher'):
 model = queryset.model
 type = apps.get_model('contenttypes', 'ContentType').objects.get_for_model(model)
 if ((user is None) or user.is_anonymous()):
  sql = 'SELECT false'
 else:
  sql = 'SELECT CASE WHEN (SELECT count(*)\n                                      FROM notifications_watched\n                                     WHERE notifications_watched.content_type_id = {type_id}\n                                       AND notifications_watched.object_id = {tbl}.id\n                                       AND notifications_watched.user_id = {user_id}) > 0\n                              THEN TRUE\n                              ELSE FALSE\n                         END'
  sql = sql.format(type_id=type.id, tbl=model._meta.db_table, user_id=user.id)
 qs = queryset.extra(select={as_field: sql})
 return qs

def attach_is_watcher_to_queryset ( queryset , user , as_field = 'is_watcher' ) : model = queryset . model type = apps . get_model ( 'contenttypes' , 'ContentType' ) . objects . get_for_model ( model ) if ( ( user is None ) or user . is_anonymous ( ) ) : sql = 'SELECT false' else : sql = 'SELECT CASE WHEN (SELECT count(*)\n                                      FROM notifications_watched\n                                     WHERE notifications_watched.content_type_id = {type_id}\n                                       AND notifications_watched.object_id = {tbl}.id\n                                       AND notifications_watched.user_id = {user_id}) > 0\n                              THEN TRUE\n                              ELSE FALSE\n                         END' sql = sql . format ( type_id = type . id , tbl = model . _meta . db_table , user_id = user . id ) qs = queryset . extra ( select = { as_field : sql } ) return qs
attach is_watcher boolean to each object of the queryset .