Skip to content

Commit 281d869

Browse files
dracosjeking3
authored andcommitted
Fix diff_against on model with non-editable fields
1 parent c76d2d7 commit 281d869

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Authors
8282
- Martin Bachwerk
8383
- Marty Alchin
8484
- Matheus Cansian (`mscansian <https://github.com/mscansian>`_)
85+
- Matthew Somerville (`dracos <https://github.com/dracos>`_)
8586
- Mauricio de Abreu Antunes
8687
- Maxim Zemskov (`MaximZemskov <https://github.com/MaximZemskov>`_)
8788
- Micah Denbraver

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Full list of changes:
2828
- Add Python 3.10 to test matrix (gh-899)
2929
- Added support for Django 4.0 (gh-898)
3030
- Dropped support for Python 3.6, which reached end-of-life on 2021-12-23 (gh-946).
31+
- Fix bug with ``history.diff_against`` with non-editable fields (gh-923)
3132

3233
3.0.0 (2021-04-16)
3334
------------------

simple_history/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,9 @@ def diff_against(self, old_history, excluded_fields=None, included_fields=None):
646646
excluded_fields = set()
647647

648648
if included_fields is None:
649-
included_fields = {f.name for f in old_history.instance_type._meta.fields}
649+
included_fields = {
650+
f.name for f in old_history.instance_type._meta.fields if f.editable
651+
}
650652

651653
fields = set(included_fields).difference(excluded_fields)
652654

simple_history/tests/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ def get_absolute_url(self):
2525
return reverse("poll-detail", kwargs={"pk": self.pk})
2626

2727

28+
class PollWithNonEditableField(models.Model):
29+
question = models.CharField(max_length=200)
30+
pub_date = models.DateTimeField("date published")
31+
modified = models.DateTimeField(auto_now=True, editable=False)
32+
33+
history = HistoricalRecords()
34+
35+
2836
class PollWithUniqueQuestion(models.Model):
2937
question = models.CharField(max_length=200, unique=True)
3038
pub_date = models.DateTimeField("date published")

simple_history/tests/tests/test_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
Place,
7979
Poll,
8080
PollInfo,
81+
PollWithNonEditableField,
8182
PollWithExcludedFieldsWithDefaults,
8283
PollWithExcludedFKField,
8384
PollWithExcludeFields,
@@ -720,6 +721,18 @@ def test_history_diff_with_included_fields(self):
720721
self.assertEqual(delta.changed_fields, ["question"])
721722
self.assertEqual(len(delta.changes), 1)
722723

724+
def test_history_diff_with_non_editable_field(self):
725+
p = PollWithNonEditableField.objects.create(
726+
question="what's up?", pub_date=today
727+
)
728+
p.question = "what's up, man?"
729+
p.save()
730+
new_record, old_record = p.history.all()
731+
with self.assertNumQueries(0):
732+
delta = new_record.diff_against(old_record)
733+
self.assertEqual(delta.changed_fields, ["question"])
734+
self.assertEqual(len(delta.changes), 1)
735+
723736
def test_history_with_unknown_field(self):
724737
p = Poll.objects.create(question="what's up?", pub_date=today)
725738
p.question = "what's up, man?"

0 commit comments

Comments
 (0)