Skip to content

Commit 2d3de6a

Browse files
xahgmahRoss Mechanic
authored andcommitted
Fixed bulk_create_with_history support for HistoryRecords with relation_name attribute (#593)
1 parent 618670f commit 2d3de6a

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
Unreleased
55
----------
6+
- Fixed bulk_create_with_history support for HistoryRecords with `relation_name` attribute (gh-591)
67
- Added support for bulk_create_with_history for databeses different from PostgreSQL (gh-577)
78
- Fixed DoesNotExist error when trying to get instance if object is deleted (gh-571)
89
- Fix `model_to_dict` to detect changes in a parent model when using

simple_history/manager.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ def _as_of_set(self, date):
101101
def bulk_history_create(self, objs, batch_size=None):
102102
"""Bulk create the history for the objects specified by objs"""
103103

104-
historical_instances = [
105-
self.model(
104+
historical_instances = []
105+
for instance in objs:
106+
row = self.model(
106107
history_date=getattr(instance, "_history_date", now()),
107108
history_user=getattr(instance, "_history_user", None),
108109
history_change_reason=getattr(instance, "changeReason", ""),
@@ -113,8 +114,9 @@ def bulk_history_create(self, objs, batch_size=None):
113114
if field.name not in self.model._history_excluded_fields
114115
}
115116
)
116-
for instance in objs
117-
]
117+
if hasattr(self.model, "history_relation"):
118+
row.history_relation_id = instance.pk
119+
historical_instances.append(row)
118120

119121
return self.model.objects.bulk_create(
120122
historical_instances, batch_size=batch_size

simple_history/tests/tests/test_utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
from mock import Mock, patch
55

66
from simple_history.exceptions import NotHistoricalModelError
7-
from simple_history.tests.models import Document, Place, Poll, PollWithExcludeFields
7+
from simple_history.tests.models import (
8+
Document,
9+
Place,
10+
Poll,
11+
PollWithExcludeFields,
12+
Street,
13+
)
814
from simple_history.utils import bulk_create_with_history
915

1016

@@ -63,6 +69,17 @@ def test_bulk_create_works_with_excluded_fields(self):
6369
self.assertEqual(PollWithExcludeFields.objects.count(), 5)
6470
self.assertEqual(PollWithExcludeFields.history.count(), 5)
6571

72+
def test_bulk_create_history_with_relation_name(self):
73+
self.data = [
74+
Street(name="Street 1"),
75+
Street(name="Street 2"),
76+
Street(name="Street 3"),
77+
Street(name="Street 4"),
78+
]
79+
bulk_create_with_history(self.data, Street)
80+
self.assertEqual(Street.objects.count(), 4)
81+
self.assertEqual(Street.log.count(), 4)
82+
6683

6784
class BulkCreateWithHistoryTransactionTestCase(TransactionTestCase):
6885
def setUp(self):

0 commit comments

Comments
 (0)