diff --git a/CHANGES.rst b/CHANGES.rst index aa827a25..0141966f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,8 @@ Unreleased - Added ``custom_historical_attrs`` to ``bulk_create_with_history()`` and ``bulk_update_with_history()`` for setting additional fields on custom history models (gh-1248) +- Passing an empty list as the ``fields`` argument to ``bulk_update_with_history()`` is + now allowed; history records will still be created (gh-1248) 3.4.0 (2023-08-18) diff --git a/docs/common_issues.rst b/docs/common_issues.rst index 26982de3..b1c9a396 100644 --- a/docs/common_issues.rst +++ b/docs/common_issues.rst @@ -119,7 +119,7 @@ A field ``session`` would be passed as ``custom_historical_attrs={'session': 'ja .. code-block:: pycon >>> bulk_update_with_history( - data, PollWithHistoricalSessionAttr, + data, PollWithHistoricalSessionAttr, [], custom_historical_attrs={'session': 'jam'} ) >>> data[0].history.latest().session diff --git a/simple_history/tests/tests/test_utils.py b/simple_history/tests/tests/test_utils.py index f18e3359..e7da5af5 100644 --- a/simple_history/tests/tests/test_utils.py +++ b/simple_history/tests/tests/test_utils.py @@ -541,7 +541,7 @@ def test_bulk_update_history_with_custom_model_attributes(self): bulk_update_with_history( self.data, PollWithHistoricalSessionAttr, - fields=["question"], + fields=[], custom_historical_attrs={"session": "training"}, ) diff --git a/simple_history/utils.py b/simple_history/utils.py index 800546dc..321ec147 100644 --- a/simple_history/utils.py +++ b/simple_history/utils.py @@ -169,7 +169,8 @@ def bulk_update_with_history( their history (all in one transaction). :param objs: List of objs of type model to be updated :param model: Model class that should be updated - :param fields: The fields that are updated + :param fields: The fields that are updated. If empty, no model objects will be + changed, but history records will still be created. :param batch_size: Number of objects that should be updated in each batch :param default_user: Optional user to specify as the history_user in each historical record @@ -189,7 +190,15 @@ def bulk_update_with_history( raise AlternativeManagerError("The given manager does not belong to the model.") with transaction.atomic(savepoint=False): - rows_updated = model_manager.bulk_update(objs, fields, batch_size=batch_size) + if not fields: + # Allow not passing any fields if the user wants to bulk-create history + # records - e.g. with `custom_historical_attrs` provided + # (Calling `bulk_update()` with no fields would have raised an error) + rows_updated = 0 + else: + rows_updated = model_manager.bulk_update( + objs, fields, batch_size=batch_size + ) history_manager.bulk_history_create( objs, batch_size=batch_size,