Skip to content

Commit 944d512

Browse files
ChrisZhangCadreRoss Mechanic
authored andcommitted
add custom_model_name to HistoricalRecords (#451)
* add custome_model_name to HistoricalRecords * add usage documentation * addressed review comments * update test cases * fix the formatting issue * updated CHANGES.rst and AUTHORS.rst
1 parent ead287a commit 944d512

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Authors
6969
- Jonathan Zvesper (@zvesp)
7070
- Matheus Cansian (@mscansian)
7171
- Jim Gomez
72+
- Hanyin Zhang
7273

7374
Background
7475
==========

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
Unreleased
5+
-----------
6+
- Add `custom_model_name` parameter to the constructor of `HistoricalRecords` (gh-451)
7+
48
2.5.1 (2018-10-19)
59
------------------
610
- Add `'+'` as the `history_type` for each instance in `bulk_history_create` (gh-449)

docs/usage.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ or
216216
)
217217
218218
219+
Custom model name
220+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
221+
222+
By default, historical model is named as 'Historical' + model name. For
223+
example, historical records for ``Choice`` is called ``HistoricalChoice``.
224+
Users can specify a custom model name via the constructor on
225+
``HistoricalRecords``. The common use case for this is avoiding naming conflict
226+
if the user already defined a model named as 'Historical' + model name.
227+
228+
.. code-block:: python
229+
230+
class ModelNameExample(models.Model):
231+
history = HistoricalRecords(
232+
custom_model_name='SimpleHistoricalModelNameExample'
233+
)
234+
235+
219236
Querying history
220237
----------------
221238

simple_history/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
4343
excluded_fields=None, history_id_field=None,
4444
history_change_reason_field=None,
4545
user_model=None, get_user=default_get_user,
46-
cascade_delete_history=False):
46+
cascade_delete_history=False, custom_model_name=None):
4747
self.user_set_verbose_name = verbose_name
4848
self.user_related_name = user_related_name
4949
self.table_name = table_name
@@ -53,6 +53,7 @@ def __init__(self, verbose_name=None, bases=(models.Model,),
5353
self.user_model = user_model
5454
self.get_user = get_user
5555
self.cascade_delete_history = cascade_delete_history
56+
self.custom_model_name = custom_model_name
5657
if excluded_fields is None:
5758
excluded_fields = []
5859
self.excluded_fields = excluded_fields
@@ -150,7 +151,8 @@ def create_history_model(self, model, inherited):
150151
attrs.update(Meta=type(str('Meta'), (), self.get_meta_options(model)))
151152
if self.table_name is not None:
152153
attrs['Meta'].db_table = self.table_name
153-
name = 'Historical%s' % model._meta.object_name
154+
name = self.custom_model_name if self.custom_model_name is not None \
155+
else 'Historical%s' % model._meta.object_name
154156
registered_models[model._meta.db_table] = model
155157
return python_2_unicode_compatible(
156158
type(str(name), self.bases, attrs))

simple_history/tests/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,10 @@ class UserTextFieldChangeReasonModel(models.Model):
531531
class CharFieldChangeReasonModel(models.Model):
532532
greeting = models.CharField(max_length=100)
533533
history = HistoricalRecords()
534+
535+
536+
class CustomNameModel(models.Model):
537+
name = models.CharField(max_length=15, unique=True)
538+
history = HistoricalRecords(
539+
custom_model_name='MyHistoricalCustomNameModel'
540+
)

simple_history/tests/tests/test_models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
Contact,
3535
ContactRegister,
3636
Country,
37+
CustomNameModel,
3738
Document,
3839
Employee,
3940
ExternalModel1,
@@ -627,6 +628,17 @@ def test_create_history_model_with_multiple_one_to_ones(self):
627628
"fields to one to one fields without throwing an "
628629
"exception.")
629630

631+
def test_instantiate_history_model_with_custom_model_name(self):
632+
try:
633+
from ..models import MyHistoricalCustomNameModel
634+
except ImportError:
635+
self.fail("MyHistoricalCustomNameModel is in wrong module")
636+
historical_model = MyHistoricalCustomNameModel()
637+
self.assertEqual(historical_model.__class__.__name__,
638+
'MyHistoricalCustomNameModel')
639+
self.assertEqual(historical_model._meta.db_table,
640+
'tests_myhistoricalcustomnamemodel')
641+
630642

631643
class AppLabelTest(TestCase):
632644
def get_table_name(self, manager):

0 commit comments

Comments
 (0)