@@ -179,6 +179,77 @@ IMPORTANT: Setting `custom_model_name` to `lambda x:f'{x}'` is not permitted.
179
179
An error will be generated and no history model created if they are the same.
180
180
181
181
182
+ Custom History Manager and Historical QuerySets
183
+ -----------------------------------------------
184
+
185
+ To manipulate the history ``Manager `` or the historical ``QuerySet `` of
186
+ ``HistoricalRecords ``, you can specify the ``history_manager `` and
187
+ ``historical_queryset `` options. The values must be subclasses
188
+ of ``simple_history.manager.HistoryManager `` and
189
+ ``simple_history.manager.HistoricalQuerySet ``, respectively.
190
+
191
+ Keep in mind, you can use either or both of these options. To understand the
192
+ difference between a ``Manager `` and a ``QuerySet ``,
193
+ see `Django's Manager documentation `_.
194
+
195
+ .. code-block :: python
196
+
197
+ from datetime import timedelta
198
+ from django.db import models
199
+ from django.utils import timezone
200
+ from simple_history.manager import HistoryManager, HistoricalQuerySet
201
+ from simple_history.models import HistoricalRecords
202
+
203
+
204
+ class HistoryQuestionManager (HistoryManager ):
205
+ def published (self ):
206
+ return self .filter(pub_date__lte = timezone.now())
207
+
208
+
209
+ class HistoryQuestionQuerySet (HistoricalQuerySet ):
210
+ def question_prefixed (self ):
211
+ return self .filter(question__startswith = " Question: " )
212
+
213
+
214
+ class Question (models .Model ):
215
+ pub_date = models.DateTimeField(" date published" )
216
+ history = HistoricalRecords(
217
+ history_manager = HistoryQuestionManager,
218
+ historical_queryset = HistoryQuestionQuerySet,
219
+ )
220
+
221
+ # This is now possible:
222
+ queryset = Question.history.published().question_prefixed()
223
+
224
+
225
+ To reuse a ``QuerySet `` from the model, see the following code example:
226
+
227
+ .. code-block :: python
228
+
229
+ from datetime import timedelta
230
+ from django.db import models
231
+ from django.utils import timezone
232
+ from simple_history.models import HistoricalRecords
233
+ from simple_history.manager import HistoryManager, HistoricalQuerySet
234
+
235
+
236
+ class QuestionQuerySet (models .QuerySet ):
237
+ def question_prefixed (self ):
238
+ return self .filter(question__startswith = " Question: " )
239
+
240
+
241
+ class HistoryQuestionQuerySet (QuestionQuerySet , HistoricalQuerySet ):
242
+ """ Redefine ``QuerySet`` with base class ``HistoricalQuerySet``."""
243
+
244
+
245
+ class Question (models .Model ):
246
+ pub_date = models.DateTimeField(" date published" )
247
+ history = HistoricalRecords(historical_queryset = HistoryQuestionQuerySet)
248
+ manager = QuestionQuerySet.as_manager()
249
+
250
+ .. _Django's Manager documentation : https://docs.djangoproject.com/en/stable/topics/db/managers/
251
+
252
+
182
253
TextField as `history_change_reason `
183
254
------------------------------------
184
255
0 commit comments