Skip to content

Commit bcf2086

Browse files
committed
Fix HistoricForeignKey when used together with prefetch_related()
1 parent 27b3dbf commit bcf2086

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changes
44
Unreleased
55
----------
66

7+
- Fixed ``HistoricForeignKey`` behaviour when used together with ``prefetch_related()``
78

89
3.3.0 (2023-03-08)
910
------------------

simple_history/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ def get_queryset(self):
860860
)
861861
else:
862862
queryset = super().get_queryset()
863-
return self._apply_rel_filters(queryset)
863+
return queryset
864864

865865
return create_reverse_many_to_one_manager(
866866
HistoricRelationModelManager, self.rel

simple_history/tests/tests/test_models.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2545,3 +2545,96 @@ def test_historic_to_historic(self):
25452545
)[0]
25462546
pt1i = pt1h.instance
25472547
self.assertEqual(pt1i.organization.name, "original")
2548+
2549+
def test_non_historic_to_historic_prefetch(self):
2550+
org1 = TestOrganizationWithHistory.objects.create(name="org1")
2551+
org2 = TestOrganizationWithHistory.objects.create(name="org2")
2552+
2553+
p1 = TestParticipantToHistoricOrganization.objects.create(
2554+
name="p1", organization=org1
2555+
)
2556+
p2 = TestParticipantToHistoricOrganization.objects.create(
2557+
name="p2", organization=org1
2558+
)
2559+
p3 = TestParticipantToHistoricOrganization.objects.create(
2560+
name="p3", organization=org2
2561+
)
2562+
p4 = TestParticipantToHistoricOrganization.objects.create(
2563+
name="p4", organization=org2
2564+
)
2565+
2566+
with self.assertNumQueries(2):
2567+
record1, record2 = TestOrganizationWithHistory.objects.prefetch_related(
2568+
"participants"
2569+
).all()
2570+
2571+
self.assertListEqual(
2572+
[p.name for p in record1.participants.all()],
2573+
[p1.name, p2.name],
2574+
)
2575+
self.assertListEqual(
2576+
[p.name for p in record2.participants.all()],
2577+
[p3.name, p4.name],
2578+
)
2579+
2580+
def test_historic_to_non_historic_prefetch(self):
2581+
org1 = TestOrganization.objects.create(name="org1")
2582+
org2 = TestOrganization.objects.create(name="org2")
2583+
2584+
p1 = TestHistoricParticipantToOrganization.objects.create(
2585+
name="p1", organization=org1
2586+
)
2587+
p2 = TestHistoricParticipantToOrganization.objects.create(
2588+
name="p2", organization=org1
2589+
)
2590+
p3 = TestHistoricParticipantToOrganization.objects.create(
2591+
name="p3", organization=org2
2592+
)
2593+
p4 = TestHistoricParticipantToOrganization.objects.create(
2594+
name="p4", organization=org2
2595+
)
2596+
2597+
with self.assertNumQueries(2):
2598+
record1, record2 = TestOrganization.objects.prefetch_related(
2599+
"participants"
2600+
).all()
2601+
2602+
self.assertListEqual(
2603+
[p.name for p in record1.participants.all()],
2604+
[p1.name, p2.name],
2605+
)
2606+
self.assertListEqual(
2607+
[p.name for p in record2.participants.all()],
2608+
[p3.name, p4.name],
2609+
)
2610+
2611+
def test_historic_to_historic_prefetch(self):
2612+
org1 = TestOrganizationWithHistory.objects.create(name="org1")
2613+
org2 = TestOrganizationWithHistory.objects.create(name="org2")
2614+
2615+
p1 = TestHistoricParticipanToHistoricOrganization.objects.create(
2616+
name="p1", organization=org1
2617+
)
2618+
p2 = TestHistoricParticipanToHistoricOrganization.objects.create(
2619+
name="p2", organization=org1
2620+
)
2621+
p3 = TestHistoricParticipanToHistoricOrganization.objects.create(
2622+
name="p3", organization=org2
2623+
)
2624+
p4 = TestHistoricParticipanToHistoricOrganization.objects.create(
2625+
name="p4", organization=org2
2626+
)
2627+
2628+
with self.assertNumQueries(2):
2629+
record1, record2 = TestOrganizationWithHistory.objects.prefetch_related(
2630+
"historic_participants"
2631+
).all()
2632+
2633+
self.assertListEqual(
2634+
[p.name for p in record1.historic_participants.all()],
2635+
[p1.name, p2.name],
2636+
)
2637+
self.assertListEqual(
2638+
[p.name for p in record2.historic_participants.all()],
2639+
[p3.name, p4.name],
2640+
)

0 commit comments

Comments
 (0)