def as_sql(self):
    qn = self.connection.ops.quote_name
    opts = self.query.get_meta()
    result = [('INSERT INTO %s' % qn(opts.db_table))]
    has_fields = bool(self.query.fields)
    fields = (self.query.fields if has_fields else [opts.pk])
    result.append(('(%s)' % ', '.join((qn(f.column) for f in fields))))
    if has_fields:
        value_rows = [[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields] for obj in self.query.objs]
    else:
        value_rows = [[self.connection.ops.pk_default_value()] for _ in self.query.objs]
        fields = [None]
    can_bulk = ((not self.return_id) and self.connection.features.has_bulk_insert)
    (placeholder_rows, param_rows) = self.assemble_as_sql(fields, value_rows)
    if (self.return_id and self.connection.features.can_return_id_from_insert):
        params = param_rows[0]
        col = ('%s.%s' % (qn(opts.db_table), qn(opts.pk.column)))
        result.append(('VALUES (%s)' % ', '.join(placeholder_rows[0])))
        (r_fmt, r_params) = self.connection.ops.return_insert_id()
        if r_fmt:
            result.append((r_fmt % col))
            params += r_params
        return [(' '.join(result), tuple(params))]
    if can_bulk:
        result.append(self.connection.ops.bulk_insert_sql(fields, placeholder_rows))
        return [(' '.join(result), tuple((p for ps in param_rows for p in ps)))]
    else:
        return [(' '.join((result + [('VALUES (%s)' % ', '.join(p))])), vals) for (p, vals) in zip(placeholder_rows, param_rows)]
def as_sql(self):
    qn = self.connection.ops.quote_name
    opts = self.query.get_meta()
    result = [('INSERT INTO %s' % qn(opts.db_table))]
    has_fields = bool(self.query.fields)
    fields = (self.query.fields if has_fields else [opts.pk])
    result.append(('(%s)' % ', '.join((qn(f.column) for f in fields))))
    if has_fields:
        value_rows = [[self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields] for obj in self.query.objs]
    else:
        value_rows = [[self.connection.ops.pk_default_value()] for _ in self.query.objs]
        fields = [None]
    can_bulk = ((not self.return_id) and self.connection.features.has_bulk_insert)
    (placeholder_rows, param_rows) = self.assemble_as_sql(fields, value_rows)
    if (self.return_id and self.connection.features.can_return_id_from_insert):
        params = param_rows[0]
        col = ('%s.%s' % (qn(opts.db_table), qn(opts.pk.column)))
        result.append(('VALUES (%s)' % ', '.join(placeholder_rows[0])))
        (r_fmt, r_params) = self.connection.ops.return_insert_id()
        if r_fmt:
            result.append((r_fmt % col))
            params += r_params
        return [(' '.join(result), tuple(params))]
    if can_bulk:
        result.append(self.connection.ops.bulk_insert_sql(fields, placeholder_rows))
        return [(' '.join(result), tuple((p for ps in param_rows for p in ps)))]
    else:
        return [(' '.join((result + [('VALUES (%s)' % ', '.join(p))])), vals) for (p, vals) in zip(placeholder_rows, param_rows)]