Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed reason field being shared between models. #1434

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Authors
- Nathan Villagaray-Carski (`ncvc <https://github.com/ncvc>`_)
- Nianpeng Li
- Nick Träger
- Noam Kushinsky (`noamkush <https://github.com/noamkush>`_)
- Noel James (`NoelJames <https://github.com/NoelJames>`_)
- Ofek Lev (`ofek <https://github.com/ofek>`_)
- Phillip Marshall
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changes
Unreleased
----------

- Fixed history_change_reason_field being shared across inherited models (gh-1433)

3.8.0 (2025-01-23)
------------------

Expand Down
5 changes: 3 additions & 2 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,9 @@ def copy_fields(self, model):

def _get_history_change_reason_field(self):
if self.history_change_reason_field:
# User specific field from init
history_change_reason_field = self.history_change_reason_field
# User specific field from init.
# It's cloned so a reason from an inherited model isn't shared.
history_change_reason_field = self.history_change_reason_field.clone()
elif getattr(
settings, "SIMPLE_HISTORY_HISTORY_CHANGE_REASON_USE_TEXT_FIELD", False
):
Expand Down
14 changes: 14 additions & 0 deletions simple_history/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,12 @@ class Meta:
abstract = True


class TrackedAbstractBaseWithReasonField(models.Model):
history = HistoricalRecords(
inherit=True, history_change_reason_field=models.TextField()
)


class UntrackedAbstractBase(models.Model):
class Meta:
abstract = True
Expand Down Expand Up @@ -709,6 +715,14 @@ class InheritTracking4(TrackedAbstractBaseA):
pass


class InheritReasonField1(TrackedAbstractBaseWithReasonField):
pass


class InheritReasonField2(TrackedAbstractBaseWithReasonField):
pass


class BasePlace(models.Model):
name = models.CharField(max_length=50)
history = HistoricalRecords(inherit=True, table_name="base_places_history")
Expand Down
25 changes: 25 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
HistoricalPollWithManyToMany_places,
HistoricalState,
InheritedRestaurant,
InheritReasonField1,
InheritReasonField2,
Library,
ManyToManyModelOther,
ModelWithCustomAttrOneToOneField,
Expand Down Expand Up @@ -686,6 +688,29 @@ def test_user_textfield_history_change_reason(self):
self.assertTrue(isinstance(field, models.TextField))
self.assertEqual(history.history_change_reason, reason)

def test_inherited_history_change_reason_update(self):
inherited1 = InheritReasonField1.history.create(
history_change_reason="reason1", id=1, history_date=datetime.now()
)
inherited2 = InheritReasonField2.history.create(
history_change_reason="reason2", id=1, history_date=datetime.now()
)

InheritReasonField1.history.update(history_change_reason="new_reason1")
InheritReasonField2.history.update(history_change_reason="new_reason2")

inherited1.refresh_from_db()
inherited2.refresh_from_db()

self.assertEqual(inherited1.history_change_reason, "new_reason1")
self.assertEqual(inherited2.history_change_reason, "new_reason2")

def test_history_change_reason_field_not_shared(self):
self.assertIsNot(
InheritReasonField1.history.model._meta.get_field("history_change_reason"),
InheritReasonField2.history.model._meta.get_field("history_change_reason"),
)

def test_history_diff_includes_changed_fields(self):
p = Poll.objects.create(question="what's up?", pub_date=today)
p.question = "what's up, man?"
Expand Down