'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 = [t for t in innerq.tables if innerq.alias_refcount[t]]
    if (((not innerq_used_tables) or (innerq_used_tables == self.tables)) and (not len(innerq.having))):
        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
            self.delete_batch(values, using)
            return
        else:
            innerq.clear_select_clause()
            innerq.select = [SelectInfo((self.get_initial_alias(), pk.column), None)]
            values = innerq
        where = self.where_class()
        where.add((Constraint(None, pk.column, pk), 'in', values), AND)
        self.where = where
    self.get_compiler(using).execute_sql(None)
