Skip to content

Commit e20cf9e

Browse files
committed
Merge pull request #135 from jwhitlock/override_manager_in_register
Allow overriding records manager class in register
2 parents 28facb0 + 3225c14 commit e20cf9e

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

simple_history/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
__version__ = '1.5.1'
44

55

6-
def register(model, app=None, manager_name='history', **records_config):
6+
def register(
7+
model, app=None, manager_name='history', records_class=None,
8+
**records_config):
79
"""
810
Create historical model for `model` and attach history manager to `model`.
911
1012
Keyword arguments:
1113
app -- App to install historical model into (defaults to model.__module__)
1214
manager_name -- class attribute name to use for historical manager
15+
records_class -- class to use for history relation (defaults to
16+
HistoricalRecords)
1317
1418
This method should be used as an alternative to attaching an
1519
`HistoricalManager` instance directly to `model`.
1620
"""
1721
from . import models
1822
if model._meta.db_table not in models.registered_models:
19-
records = models.HistoricalRecords(**records_config)
23+
if records_class is None:
24+
records_class = models.HistoricalRecords
25+
records = records_class(**records_config)
2026
records.manager_name = manager_name
2127
records.module = app and ("%s.models" % app) or model.__module__
2228
records.add_extra_methods(model)

simple_history/tests/models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ class Choice(models.Model):
5454
register(Choice)
5555

5656

57+
class Voter(models.Model):
58+
user = models.ForeignKey(User)
59+
choice = models.ForeignKey(Choice, related_name='voters')
60+
61+
62+
class HistoricalRecordsVerbose(HistoricalRecords):
63+
def get_extra_fields(self, model, fields):
64+
def verbose_str(self):
65+
return '%s changed by %s as of %s' % (
66+
self.history_object, self.history_user, self.history_date)
67+
68+
extra_fields = super(
69+
HistoricalRecordsVerbose, self).get_extra_fields(model, fields)
70+
extra_fields['__str__'] = verbose_str
71+
return extra_fields
72+
73+
register(Voter, records_class=HistoricalRecordsVerbose)
74+
75+
5776
class Place(models.Model):
5877
name = models.CharField(max_length=100)
5978

simple_history/tests/tests/test_models.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
from simple_history.models import HistoricalRecords, convert_auto_field
2121
from simple_history import register
2222
from ..models import (
23-
AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Restaurant, Person,
24-
FileModel, Document, Book, HistoricalPoll, Library, State, AbstractBase,
25-
ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
23+
AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Voter, Restaurant,
24+
Person, FileModel, Document, Book, HistoricalPoll, Library, State,
25+
AbstractBase, ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
2626
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
2727
HistoricalState, HistoricalCustomFKError
2828
)
@@ -305,6 +305,16 @@ def test_reregister(self):
305305
self.assertTrue(hasattr(User, 'histories'))
306306
self.assertFalse(hasattr(User, 'again'))
307307

308+
def test_register_custome_records(self):
309+
self.assertEqual(len(Voter.history.all()), 0)
310+
poll = Poll.objects.create(pub_date=today)
311+
choice = Choice.objects.create(poll=poll, votes=0)
312+
user = User.objects.create(username='voter')
313+
voter = Voter.objects.create(choice=choice, user=user)
314+
self.assertEqual(len(voter.history.all()), 1)
315+
expected = 'Voter object changed by None as of '
316+
self.assertEqual(expected, str(voter.history.all()[0])[:len(expected)])
317+
308318

309319
class CreateHistoryModelTests(TestCase):
310320

0 commit comments

Comments
 (0)