Skip to content

Commit 9e95fd8

Browse files
uhurusurfaRoss Mechanic
authored andcommitted
Set app label via instantiation of historical records (#486)
* GH Issue 485 - add ability to override default app_label in generated history model * GH Issue 485 - add test model to test override default app_label in generated history model * GH Issue 485 - add test for ability to override default app_label in generated history model * GH Issue 485 - document ability to override default app_label in generated history model * Add name to list * Document enhancement
1 parent 8e13553 commit 9e95fd8

File tree

6 files changed

+41
-0
lines changed

6 files changed

+41
-0
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Authors
7373
- Jim Gomez
7474
- Hanyin Zhang
7575
- James Muranga (@jamesmura)
76+
- Christopher Broderick (@uhurusurfa)
7677

7778
Background
7879
==========

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changes
33

44
Unreleased
55
----------
6+
- Add `app` parameter to the constructor of `HistoricalRecords` (gh-486)
67
- Add `custom_model_name` parameter to the constructor of `HistoricalRecords` (gh-451)
78
- Fix header on history pages when custom site_header is used (gh-448)
89
- Modify `pre_create_historircal_record` to pass `history_instance` for ease of customization (gh-421)

docs/advanced.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,3 +541,24 @@ To connect the signals to your callbacks, you can use the @receiver decorator:
541541
@receiver(post_create_historical_record)
542542
def post_create_historical_record_callback(sender, **kwargs):
543543
print("Sent after saving historical record")
544+
545+
History Model In Different App
546+
------------------------------
547+
548+
By default the app_label for the history model is the same as the base model.
549+
In some circumstances you may want to have the history models belong in a different app.
550+
This will support creating history models in a different database to the base model using
551+
database routing functionality based on app_label.
552+
To configure history models in a different app, add this to the HistoricalRecords instantiation
553+
or the record invocation: ``app="SomeAppName"``.
554+
555+
.. code-block:: python
556+
557+
class Poll(models.Model):
558+
question = models.CharField(max_length=200)
559+
history = HistoricalRecords(app="SomeAppName")
560+
561+
class Opinion(models.Model):
562+
opinion = models.CharField(max_length=2000)
563+
564+
register(Opinion, app="SomeAppName")

simple_history/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def __init__(
5050
get_user=default_get_user,
5151
cascade_delete_history=False,
5252
custom_model_name=None,
53+
app=None,
5354
):
5455
self.user_set_verbose_name = verbose_name
5556
self.user_related_name = user_related_name
@@ -61,6 +62,7 @@ def __init__(
6162
self.get_user = get_user
6263
self.cascade_delete_history = cascade_delete_history
6364
self.custom_model_name = custom_model_name
65+
self.app = app
6466
if excluded_fields is None:
6567
excluded_fields = []
6668
self.excluded_fields = excluded_fields
@@ -340,6 +342,8 @@ def get_meta_options(self, model):
340342
else:
341343
name = format_lazy("historical {}", smart_text(model._meta.verbose_name))
342344
meta_fields["verbose_name"] = name
345+
if self.app:
346+
meta_fields["app_label"] = self.app
343347
return meta_fields
344348

345349
def post_save(self, instance, created, **kwargs):

simple_history/registry_tests/tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
UUIDRegisterModel,
2929
Voter,
3030
ModelWithCustomAttrForeignKey,
31+
ModelWithHistoryInDifferentApp,
3132
)
3233

3334
get_model = apps.get_model
@@ -211,3 +212,11 @@ def test_migrate_command(self):
211212
management.call_command(
212213
"migrate", "migration_test_app", fake=True, stdout=StringIO()
213214
)
215+
216+
217+
class TestModelWithHistoryInDifferentApp(TestCase):
218+
""" https://github.com/treyhunner/django-simple-history/issues/485 """
219+
220+
def test__different_app(self):
221+
appLabel = ModelWithHistoryInDifferentApp.history.model._meta.app_label
222+
self.assertEqual(appLabel, "external")

simple_history/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ class ContactRegister(models.Model):
356356
register(ContactRegister, table_name="contacts_register_history")
357357

358358

359+
class ModelWithHistoryInDifferentApp(models.Model):
360+
name = models.CharField(max_length=30)
361+
history = HistoricalRecords(app="external")
362+
363+
359364
###############################################################################
360365
#
361366
# Inheritance examples

0 commit comments

Comments
 (0)