diff --git a/CHANGES.rst b/CHANGES.rst index f92426f3e..c19a7ff8b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -14,6 +14,7 @@ Unreleased - Dropped support for Python 3.7, which reached end-of-life on 2023-06-27 (gh-1202) - Dropped support for Django 4.0, which reached end-of-life on 2023-04-01 (gh-1202) - Added support for Django 4.2 (gh-1202) +- Made ``bulk_update_with_history()`` return the number of model rows updated (gh-1206) 3.3.0 (2023-03-08) ------------------ diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index 0b629a439..7fc497fd8 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -1,6 +1,8 @@ from datetime import datetime +from unittest import skipUnless from unittest.mock import Mock, patch +import django from django.contrib.auth import get_user_model from django.db import IntegrityError, transaction from django.test import TestCase, TransactionTestCase, override_settings @@ -423,6 +425,15 @@ def test_bulk_update_history_with_batch_size(self): self.assertEqual(Poll.objects.count(), 5) self.assertEqual(Poll.history.filter(history_type="~").count(), 5) + @skipUnless(django.VERSION >= (4, 0), "Requires Django 4.0 or above") + def test_bulk_update_with_history_returns_rows_updated(self): + rows_updated = bulk_update_with_history( + self.data, + Poll, + fields=["question"], + ) + self.assertEqual(rows_updated, 5) + class BulkUpdateWithHistoryAlternativeManagersTestCase(TestCase): def setUp(self): diff --git a/simple_history/utils.py b/simple_history/utils.py index a3b405bc0..d74a91d83 100644 --- a/simple_history/utils.py +++ b/simple_history/utils.py @@ -173,6 +173,7 @@ def bulk_update_with_history( record :param manager: Optional model manager to use for the model instead of the default manager + :return: The number of model rows updated, not including any history objects """ history_manager = get_history_manager_for_model(model) model_manager = manager or model._default_manager @@ -180,7 +181,7 @@ def bulk_update_with_history( raise AlternativeManagerError("The given manager does not belong to the model.") with transaction.atomic(savepoint=False): - model_manager.bulk_update(objs, fields, batch_size=batch_size) + rows_updated = model_manager.bulk_update(objs, fields, batch_size=batch_size) history_manager.bulk_history_create( objs, batch_size=batch_size, @@ -189,6 +190,7 @@ def bulk_update_with_history( default_change_reason=default_change_reason, default_date=default_date, ) + return rows_updated def get_change_reason_from_object(obj):