|
1 | 1 | Common Issues
|
2 | 2 | =============
|
3 | 3 |
|
| 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 | + |
4 | 55 | Tracking Custom Users
|
5 | 56 | ---------------------
|
6 | 57 |
|
@@ -34,7 +85,7 @@ up the request. To solve this issue, add the following code to any
|
34 | 85 | ``clean_environment`` or ``tearDown`` method that
|
35 | 86 | you use:
|
36 | 87 |
|
37 |
| -.. code-block::python |
| 88 | +.. code-block:: python |
38 | 89 |
|
39 | 90 | from simple_history.middleware import HistoricalRecords
|
40 | 91 | if hasattr(HistoricalRecords.thread, 'request'):
|
|
0 commit comments