Tomasz Wysocki Tomasz Wysocki - 7 months ago 8
SQL Question

How many rows were deleted?

Is it possible to check how many rows were deleted by a query?

queryset = MyModel.object.filter(foo=bar)
queryset.delete()
deleted = ...


Or should I use transactions for that?

@transaction.commit_on_success
def delete_some_rows():
queryset = MyModel.object.filter(foo=bar)
deleted = queryset.count()
queryset.delete()


PHP + MySQL example:

mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n", mysql_affected_rows());

Answer

I don't understand why queryset.count() or queryset.distinct().count() isn't enough.