Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Authors
- Marty Alchin
- Mauricio de Abreu Antunes
- Micah Denbraver
- Miguel Vargas
- Nianpeng Li
- Phillip Marshall
- Rajesh Pappula
- Roberto Aguilar
Expand Down
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changes
=======

Unreleased
-----------
- Add `'+'` as the `history_type` for each instance in `bulk_history_create` (gh-449)
- Add support for `history_change_reason` for each instance in `bulk_history_create` (gh-449)

2.5.0 (2018-10-18)
------------------
- Add ability to cascade delete historical records when master record is deleted (gh-440)
Expand Down
12 changes: 12 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ history:
>>> Poll.history.count()
1000

If you want to specify a change reason for each record in the bulk create, you
can add `changeReason` on each instance:

.. code-block:: pycon

>>> for poll in data:
poll.changeReason = 'reason'
>>> objs = bulk_create_with_history(data, Poll, batch_size=500)
>>> Poll.history.get(id=data[0].id).history_change_reason
'reason'


QuerySet Updates with History
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unlike with ``bulk_create``, `queryset updates`_ perform an SQL update query on
Expand Down
2 changes: 2 additions & 0 deletions simple_history/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def bulk_history_create(self, objs, batch_size=None):
self.model(
history_date=getattr(instance, '_history_date', now()),
history_user=getattr(instance, '_history_user', None),
history_change_reason=getattr(instance, 'changeReason', ''),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

history_type='+',
**{
field.attname: getattr(instance, field.attname)
for field in instance._meta.fields
Expand Down
15 changes: 15 additions & 0 deletions simple_history/tests/tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,26 @@ def test_simple_bulk_history_create(self):
self.assertQuerysetEqual(Poll.history.order_by('question'), [
'Question 1', 'Question 2', 'Question 3', 'Question 4'
], attrgetter('question'))
self.assertTrue(
all([history.history_type == '+'
for history in Poll.history.all()])
)

created = Poll.history.bulk_create([])
self.assertEqual(created, [])
self.assertEqual(Poll.history.count(), 4)

def test_bulk_history_create_with_change_reason(self):
for poll in self.data:
poll.changeReason = 'reason'

Poll.history.bulk_history_create(self.data)

self.assertTrue(
all([history.history_change_reason == 'reason'
for history in Poll.history.all()])
)

def test_bulk_history_create_on_objs_without_ids(self):
self.data = [
Poll(question='Question 1', pub_date=datetime.now()),
Expand Down