Skip to content

Commit 4b1ba15

Browse files
author
Ross Mechanic
authored
Updated documentation for queryset updates (#414)
* Updated documentation for queryset updates * Fixed error where code-blocks weren't showing
1 parent ff94161 commit 4b1ba15

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

docs/reference.rst

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,57 @@
11
Common Issues
22
=============
33

4+
Bulk Creating and Queryset Updating
5+
-----------------------------------
6+
Django Simple History functions by saving history using a ``post_save`` signal
7+
every time that an object with history is saved. However, for certain bulk
8+
operations, such as bulk_create_ and `queryset updates <https://docs.djangoproject.com/en/2.0/ref/models/querysets/#update>`_,
9+
signals are not sent, and the history is not saved automatically. However,
10+
Django Simple History provides utility functions to work around this.
11+
12+
Bulk Creating a Model with History
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
As of Django Simple History 2.2.0, we can use the utility function
15+
``bulk_create_with_history`` in order to bulk create objects while saving their
16+
history:
17+
18+
.. _bulk_create: https://docs.djangoproject.com/en/2.0/ref/models/querysets/#bulk-create
19+
20+
21+
.. code-block:: pycon
22+
23+
>>> from simple_history.utils import bulk_create_with_history
24+
>>> from simple_history.tests.models import Poll
25+
>>> from django.utils.timezone import now
26+
>>>
27+
>>> data = [Poll(id=x, question='Question ' + str(x), pub_date=now()) for x in range(1000)]
28+
>>> objs = bulk_create_with_history(data, Poll, batch_size=500)
29+
>>> Poll.objects.count()
30+
1000
31+
>>> Poll.history.count()
32+
1000
33+
34+
QuerySet Updates with History
35+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36+
Unlike with ``bulk_create``, `queryset updates`_ perform an SQL update query on
37+
the queryset, and never return the actual updated objects (which would be
38+
necessary for the inserts into the historical table). Thus, we tell you that
39+
queryset updates will not save history (since no ``post_save`` signal is sent).
40+
As the Django documentation says::
41+
42+
If you want to update a bunch of records for a model that has a custom
43+
``save()`` method, loop over them and call ``save()``, like this:
44+
45+
.. code-block:: python
46+
47+
for e in Entry.objects.filter(pub_date__year=2010):
48+
e.comments_on = False
49+
e.save()
50+
51+
.. _queryset updates: https://docs.djangoproject.com/en/2.0/ref/models/querysets/#update
52+
53+
54+
455
Tracking Custom Users
556
---------------------
657

@@ -34,7 +85,7 @@ up the request. To solve this issue, add the following code to any
3485
``clean_environment`` or ``tearDown`` method that
3586
you use:
3687

37-
.. code-block::python
88+
.. code-block:: python
3889
3990
from simple_history.middleware import HistoricalRecords
4091
if hasattr(HistoricalRecords.thread, 'request'):

docs/usage.rst

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -311,33 +311,3 @@ And to revert to that ``HistoricalPoll`` instance, we can do:
311311
This will change the ``poll`` instance to have the data from the
312312
``HistoricalPoll`` object and it will create a new row in the
313313
``HistoricalPoll`` table indicating that a new change has been made.
314-
315-
Bulk Creating and Queryset Updating
316-
-----------------------------------
317-
Django Simple History functions by saving history using a ``post_save`` signal
318-
every time that an object with history is saved. However, for certain bulk
319-
operations, such as bulk_create_ and `queryset updates <https://docs.djangoproject.com/en/2.0/ref/models/querysets/#update>`_,
320-
signals are not sent, and the history is not saved automatically. However,
321-
Django Simple History provides utility functions to work around this.
322-
323-
Bulk Creating a Model with History
324-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
325-
As of Django Simple History 2.2.0, we can use the utility function
326-
``bulk_create_with_history`` in order to bulk create objects while saving their
327-
history:
328-
329-
.. _bulk_create: https://docs.djangoproject.com/en/2.0/ref/models/querysets/#bulk-create
330-
331-
332-
.. code-block:: pycon
333-
334-
>>> from simple_history.utils import bulk_create_with_history
335-
>>> from simple_history.tests.models import Poll
336-
>>> from django.utils.timezone import now
337-
>>>
338-
>>> data = [Poll(id=x, question='Question ' + str(x), pub_date=now()) for x in range(1000)]
339-
>>> objs = bulk_create_with_history(data, Poll, batch_size=500)
340-
>>> Poll.objects.count()
341-
1000
342-
>>> Poll.history.count()
343-
1000

0 commit comments

Comments
 (0)