@@ -65,6 +65,7 @@ def bulk_create_with_history(
65
65
default_user = None ,
66
66
default_change_reason = None ,
67
67
default_date = None ,
68
+ custom_historical_attrs = None ,
68
69
):
69
70
"""
70
71
Bulk create the objects specified by objs while also bulk creating
@@ -81,6 +82,8 @@ def bulk_create_with_history(
81
82
in each historical record
82
83
:param default_date: Optional date to specify as the history_date in each historical
83
84
record
85
+ :param custom_historical_attrs: Optional dict of field `name`:`value` to specify
86
+ values for custom fields
84
87
:return: List of objs with IDs
85
88
"""
86
89
# Exclude ManyToManyFields because they end up as invalid kwargs to
@@ -106,6 +109,7 @@ def bulk_create_with_history(
106
109
default_user = default_user ,
107
110
default_change_reason = default_change_reason ,
108
111
default_date = default_date ,
112
+ custom_historical_attrs = custom_historical_attrs ,
109
113
)
110
114
if second_transaction_required :
111
115
with transaction .atomic (savepoint = False ):
@@ -143,6 +147,7 @@ def bulk_create_with_history(
143
147
default_user = default_user ,
144
148
default_change_reason = default_change_reason ,
145
149
default_date = default_date ,
150
+ custom_historical_attrs = custom_historical_attrs ,
146
151
)
147
152
objs_with_id = obj_list
148
153
return objs_with_id
@@ -157,13 +162,15 @@ def bulk_update_with_history(
157
162
default_change_reason = None ,
158
163
default_date = None ,
159
164
manager = None ,
165
+ custom_historical_attrs = None ,
160
166
):
161
167
"""
162
168
Bulk update the objects specified by objs while also bulk creating
163
169
their history (all in one transaction).
164
170
:param objs: List of objs of type model to be updated
165
171
:param model: Model class that should be updated
166
- :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.
167
174
:param batch_size: Number of objects that should be updated in each batch
168
175
:param default_user: Optional user to specify as the history_user in each historical
169
176
record
@@ -173,6 +180,8 @@ def bulk_update_with_history(
173
180
record
174
181
:param manager: Optional model manager to use for the model instead of the default
175
182
manager
183
+ :param custom_historical_attrs: Optional dict of field `name`:`value` to specify
184
+ values for custom fields
176
185
:return: The number of model rows updated, not including any history objects
177
186
"""
178
187
history_manager = get_history_manager_for_model (model )
@@ -181,14 +190,23 @@ def bulk_update_with_history(
181
190
raise AlternativeManagerError ("The given manager does not belong to the model." )
182
191
183
192
with transaction .atomic (savepoint = False ):
184
- 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
+ )
185
202
history_manager .bulk_history_create (
186
203
objs ,
187
204
batch_size = batch_size ,
188
205
update = True ,
189
206
default_user = default_user ,
190
207
default_change_reason = default_change_reason ,
191
208
default_date = default_date ,
209
+ custom_historical_attrs = custom_historical_attrs ,
192
210
)
193
211
return rows_updated
194
212
0 commit comments