'Delete the queryset in one SQL query (if possible). For simple queries
this is done by copying the query.query.where to self.query, for
complex queries by using subquery.'

def delete_qs(self, query, using):

    innerq = query.query
    innerq.get_initial_alias()
    self.get_initial_alias()
    innerq_used_tables = tuple([t for t in innerq.alias_map if innerq.alias_refcount[t]])
    if ((not innerq_used_tables) or (innerq_used_tables == tuple(self.alias_map))):
        self.where = innerq.where
    else:
        pk = query.model._meta.pk
        if (not connections[using].features.update_can_self_select):
            values = list(query.values_list('pk', flat=True))
            if (not values):
                return 0
            return self.delete_batch(values, using)
        else:
            innerq.clear_select_clause()
            innerq.select = [pk.get_col(self.get_initial_alias())]
            values = innerq
        self.where = self.where_class()
        self.add_q(Q(pk__in=values))
    cursor = self.get_compiler(using).execute_sql(CURSOR)
    return (cursor.rowcount if cursor else 0)
