Skip to content

Commit 65785a0

Browse files
Alig1493Ross Mechanic
authored andcommitted
Most recent with excluded fields (#477)
* Added changes in manager to compensate for excluded fields while feetching most recent. Updated test_model_with_excluded_fields test to verify. * Requirement changes. * hot fix * Formatted test_models file * Refactored exclude fields to another method. * Requested changes for excluded fields * Getting history excluded fields attribute if it exists * changed nested ifs
1 parent 507fa7d commit 65785a0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

simple_history/manager.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def get_queryset(self):
3434
key_name = self.instance._meta.pk.name
3535
return self.get_super_queryset().filter(**{key_name: self.instance.pk})
3636

37+
def get_excluded_fields(self):
38+
return getattr(self.model, "_history_excluded_fields", [])
39+
3740
def most_recent(self):
3841
"""
3942
Returns the most recent copy of the instance available in the history.
@@ -45,7 +48,11 @@ def most_recent(self):
4548
)
4649
)
4750
tmp = []
51+
excluded_fields = self.get_excluded_fields()
52+
4853
for field in self.instance._meta.fields:
54+
if field.name in excluded_fields:
55+
continue
4956
if isinstance(field, models.ForeignKey):
5057
tmp.append(field.name + "_id")
5158
else:

simple_history/tests/tests/test_models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ def test_model_with_excluded_fields(self):
445445
self.assertIn("question", all_fields_names)
446446
self.assertNotIn("pub_date", all_fields_names)
447447

448+
most_recent = p.history.most_recent()
449+
self.assertIn("question", all_fields_names)
450+
self.assertNotIn("pub_date", all_fields_names)
451+
self.assertEqual(most_recent.__class__, PollWithExcludeFields)
452+
self.assertIn("pub_date", history._history_excluded_fields)
453+
448454
def test_user_model_override(self):
449455
user1 = User.objects.create_user("user1", "[email protected]")
450456
user2 = User.objects.create_user("user2", "[email protected]")

0 commit comments

Comments
 (0)