Skip to content

Commit 04888fe

Browse files
committed
Merge pull request #145 from treyhunner/foreignkey-primarykey
Fix primary key handling when primary key is a `ForeignKey`
2 parents 7de8b1c + 51e7a25 commit 04888fe

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

simple_history/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def get_queryset(self):
3030
if self.instance is None:
3131
return qs
3232

33-
if isinstance(self.instance._meta.pk, models.OneToOneField):
33+
if isinstance(self.instance._meta.pk, models.ForeignKey):
3434
key_name = self.instance._meta.pk.name + "_id"
3535
else:
3636
key_name = self.instance._meta.pk.name

simple_history/tests/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,8 @@ class SeriesWork(models.Model):
232232

233233
class Meta:
234234
order_with_respect_to = 'series'
235+
236+
237+
class PollInfo(models.Model):
238+
poll = models.ForeignKey(Poll, primary_key=True)
239+
history = HistoricalRecords()

simple_history/tests/tests/test_models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Person, FileModel, Document, Book, HistoricalPoll, Library, State,
2626
AbstractBase, ConcreteAttr, ConcreteUtil, SelfFK, Temperature, WaterLevel,
2727
ExternalModel1, ExternalModel3, UnicodeVerboseName, HistoricalChoice,
28-
HistoricalState, HistoricalCustomFKError, Series, SeriesWork
28+
HistoricalState, HistoricalCustomFKError, Series, SeriesWork, PollInfo
2929
)
3030
from ..external.models import ExternalModel2, ExternalModel4
3131

@@ -282,6 +282,13 @@ def test_historical_verbose_name_follows_model_verbose_name(self):
282282
self.assertEqual('historical quiet please',
283283
l.history.get()._meta.verbose_name)
284284

285+
def test_foreignkey_primarykey(self):
286+
"""Test saving a tracked model with a `ForeignKey` primary key."""
287+
poll = Poll(pub_date=today)
288+
poll.save()
289+
poll_info = PollInfo(poll=poll)
290+
poll_info.save()
291+
285292

286293
class RegisterTest(TestCase):
287294
def test_register_no_args(self):

0 commit comments

Comments
 (0)