Skip to content

Commit ff08965

Browse files
AmandaCLNgRoss Mechanic
authored andcommitted
Fixed bug in most_recent() where excluded_fields weren't handled correctly (#562)
* fixed most_recent bug #561 and updated tests * updated AUTHORS.rst * updated CHANGES.rst * make format reformatting
1 parent aa5874b commit ff08965

File tree

6 files changed

+22
-4
lines changed

6 files changed

+22
-4
lines changed

AUTHORS.rst

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Authors
1414
- Adnan Umer (`uadnan <https://github.com/uadnan>`_)
1515
- Aleksey Kladov
1616
- Alexander Anikeev
17+
- Amanda Ng (`AmandaCLNg <https://github.com/AmandaCLNg>`_)
1718
- Ben Lawson (`blawson <https://github.com/blawson>`_)
1819
- `bradford281 <https://github.com/bradford281>`_
1920
- Brian Armstrong (`barm <https://github.com/barm>`_)

CHANGES.rst

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

4+
- Fixed most_recent() bug with excluded_fields (gh-561)
5+
6+
47
2.7.2 (2019-04-17)
58
------------------
69
- Fixed ModuleNotFound issue for `six` (gh-553)

simple_history/manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def most_recent(self):
5656
tmp.append(field.name)
5757
fields = tuple(tmp)
5858
try:
59-
values = self.get_queryset().values_list(*fields)[0]
59+
values = self.get_queryset().values(*fields)[0]
6060
except IndexError:
6161
raise self.instance.DoesNotExist(
6262
"%s has no historical record." % self.instance._meta.object_name
6363
)
64-
return self.instance.__class__(*values)
64+
return self.instance.__class__(**values)
6565

6666
def as_of(self, date):
6767
"""Get a snapshot as of a specific date.

simple_history/tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def get_absolute_url(self):
3030
class PollWithExcludeFields(models.Model):
3131
question = models.CharField(max_length=200)
3232
pub_date = models.DateTimeField("date published")
33+
place = models.TextField(null=True)
3334

3435
history = HistoricalRecords(excluded_fields=["pub_date"])
3536

simple_history/tests/tests/test_models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,18 +463,24 @@ def test_foreignkey_primarykey(self):
463463
poll_info.save()
464464

465465
def test_model_with_excluded_fields(self):
466-
p = PollWithExcludeFields(question="what's up?", pub_date=today)
466+
p = PollWithExcludeFields(
467+
question="what's up?", pub_date=today, place="The Pub"
468+
)
467469
p.save()
468470
history = PollWithExcludeFields.history.all()[0]
469471
all_fields_names = [f.name for f in history._meta.fields]
470472
self.assertIn("question", all_fields_names)
471473
self.assertNotIn("pub_date", all_fields_names)
474+
self.assertEqual(history.question, p.question)
475+
self.assertEqual(history.place, p.place)
472476

473477
most_recent = p.history.most_recent()
474478
self.assertIn("question", all_fields_names)
475479
self.assertNotIn("pub_date", all_fields_names)
476480
self.assertEqual(most_recent.__class__, PollWithExcludeFields)
477481
self.assertIn("pub_date", history._history_excluded_fields)
482+
self.assertEqual(most_recent.question, p.question)
483+
self.assertEqual(most_recent.place, p.place)
478484

479485
def test_user_model_override(self):
480486
user1 = User.objects.create_user("user1", "[email protected]")

simple_history/tests/tests/test_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def setUp(self):
1717
Poll(id=4, question="Question 4", pub_date=now()),
1818
Poll(id=5, question="Question 5", pub_date=now()),
1919
]
20+
self.data_with_excluded_fields = [
21+
PollWithExcludeFields(id=1, question="Question 1", pub_date=now()),
22+
PollWithExcludeFields(id=2, question="Question 2", pub_date=now()),
23+
PollWithExcludeFields(id=3, question="Question 3", pub_date=now()),
24+
PollWithExcludeFields(id=4, question="Question 4", pub_date=now()),
25+
PollWithExcludeFields(id=5, question="Question 5", pub_date=now()),
26+
]
2027

2128
def test_bulk_create_history(self):
2229
bulk_create_with_history(self.data, Poll)
@@ -48,7 +55,7 @@ def test_bulk_create_history_with_batch_size(self):
4855
self.assertEqual(Poll.history.count(), 5)
4956

5057
def test_bulk_create_works_with_excluded_fields(self):
51-
bulk_create_with_history(self.data, PollWithExcludeFields)
58+
bulk_create_with_history(self.data_with_excluded_fields, PollWithExcludeFields)
5259

5360
self.assertEqual(Poll.objects.count(), 0)
5461
self.assertEqual(Poll.history.count(), 0)

0 commit comments

Comments
 (0)