if distinct_ids != []: ids = "("+", ".join(distinct_ids)+")" commented_photos = Photo.objects.extra(where=['id IN '+ids])