Skip to content

Commit afd02c3

Browse files
zhaojiejoeqcloudpre-commit-ci[bot]ddabble
authored
Update utils.py with adding a return of function bulk_update_with_history (#1206)
* Update utils.py keep the original returns of bulk_update * feat: add bulk_update row_updated testcase * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply suggestions from code review Co-authored-by: Anders <[email protected]> * Update test_utils.py fix suggestion * Added changes in PR #1206 to changelog --------- Co-authored-by: qcloud <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Anders <[email protected]>
1 parent 56dc4f9 commit afd02c3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Unreleased
1414
- Dropped support for Python 3.7, which reached end-of-life on 2023-06-27 (gh-1202)
1515
- Dropped support for Django 4.0, which reached end-of-life on 2023-04-01 (gh-1202)
1616
- Added support for Django 4.2 (gh-1202)
17+
- Made ``bulk_update_with_history()`` return the number of model rows updated (gh-1206)
1718

1819
3.3.0 (2023-03-08)
1920
------------------

simple_history/tests/tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime
2+
from unittest import skipUnless
23
from unittest.mock import Mock, patch
34

5+
import django
46
from django.contrib.auth import get_user_model
57
from django.db import IntegrityError, transaction
68
from django.test import TestCase, TransactionTestCase, override_settings
@@ -423,6 +425,15 @@ def test_bulk_update_history_with_batch_size(self):
423425
self.assertEqual(Poll.objects.count(), 5)
424426
self.assertEqual(Poll.history.filter(history_type="~").count(), 5)
425427

428+
@skipUnless(django.VERSION >= (4, 0), "Requires Django 4.0 or above")
429+
def test_bulk_update_with_history_returns_rows_updated(self):
430+
rows_updated = bulk_update_with_history(
431+
self.data,
432+
Poll,
433+
fields=["question"],
434+
)
435+
self.assertEqual(rows_updated, 5)
436+
426437

427438
class BulkUpdateWithHistoryAlternativeManagersTestCase(TestCase):
428439
def setUp(self):

simple_history/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ def bulk_update_with_history(
173173
record
174174
:param manager: Optional model manager to use for the model instead of the default
175175
manager
176+
:return: The number of model rows updated, not including any history objects
176177
"""
177178
history_manager = get_history_manager_for_model(model)
178179
model_manager = manager or model._default_manager
179180
if model_manager.model is not model:
180181
raise AlternativeManagerError("The given manager does not belong to the model.")
181182

182183
with transaction.atomic(savepoint=False):
183-
model_manager.bulk_update(objs, fields, batch_size=batch_size)
184+
rows_updated = model_manager.bulk_update(objs, fields, batch_size=batch_size)
184185
history_manager.bulk_history_create(
185186
objs,
186187
batch_size=batch_size,
@@ -189,6 +190,7 @@ def bulk_update_with_history(
189190
default_change_reason=default_change_reason,
190191
default_date=default_date,
191192
)
193+
return rows_updated
192194

193195

194196
def get_change_reason_from_object(obj):

0 commit comments

Comments
 (0)