Skip to content

Commit c1ec612

Browse files
committed
Allow empty fields to bulk_update_with_history
This fixes a "hack" for when a user only wants to provide the `custom_historical_attrs` argument, where a list with arbitrary field names could be provided as a circumvention for the error that was raised otherwise - even if no model fields were actually changed.
1 parent 1032b40 commit c1ec612

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Unreleased
1010
- Added ``custom_historical_attrs`` to ``bulk_create_with_history()`` and
1111
``bulk_update_with_history()`` for setting additional fields on custom history models
1212
(gh-1248)
13+
- Passing an empty list as the ``fields`` argument to ``bulk_update_with_history()`` is
14+
now allowed; history records will still be created (gh-1248)
1315

1416

1517
3.4.0 (2023-08-18)

docs/common_issues.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ A field ``session`` would be passed as ``custom_historical_attrs={'session': 'ja
119119
.. code-block:: pycon
120120
121121
>>> bulk_update_with_history(
122-
data, PollWithHistoricalSessionAttr,
122+
data, PollWithHistoricalSessionAttr, [],
123123
custom_historical_attrs={'session': 'jam'}
124124
)
125125
>>> data[0].history.latest().session

simple_history/tests/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def test_bulk_update_history_with_custom_model_attributes(self):
541541
bulk_update_with_history(
542542
self.data,
543543
PollWithHistoricalSessionAttr,
544-
fields=["question"],
544+
fields=[],
545545
custom_historical_attrs={"session": "training"},
546546
)
547547

simple_history/utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def bulk_update_with_history(
169169
their history (all in one transaction).
170170
:param objs: List of objs of type model to be updated
171171
:param model: Model class that should be updated
172-
:param fields: The fields that are updated
172+
:param fields: The fields that are updated. If empty, no model objects will be
173+
changed, but history records will still be created.
173174
:param batch_size: Number of objects that should be updated in each batch
174175
:param default_user: Optional user to specify as the history_user in each historical
175176
record
@@ -189,7 +190,15 @@ def bulk_update_with_history(
189190
raise AlternativeManagerError("The given manager does not belong to the model.")
190191

191192
with transaction.atomic(savepoint=False):
192-
rows_updated = model_manager.bulk_update(objs, fields, batch_size=batch_size)
193+
if not fields:
194+
# Allow not passing any fields if the user wants to bulk-create history
195+
# records - e.g. with `custom_historical_attrs` provided
196+
# (Calling `bulk_update()` with no fields would have raised an error)
197+
rows_updated = 0
198+
else:
199+
rows_updated = model_manager.bulk_update(
200+
objs, fields, batch_size=batch_size
201+
)
193202
history_manager.bulk_history_create(
194203
objs,
195204
batch_size=batch_size,

0 commit comments

Comments
 (0)