From 04b15022e8d1f49af69d8a1e6cd678f31f1280ff Mon Sep 17 00:00:00 2001 From: roman_p Date: Thu, 17 Nov 2022 17:54:30 +0300 Subject: [PATCH] Fixed #26261 -- Fixed queryset crash when excluding reverse GenericRelation. Thanks Amir Hadi for the report. --- django/contrib/contenttypes/fields.py | 2 +- tests/generic_relations_regress/tests.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py index 72b9e7631a8..35fcd0d9085 100644 --- a/django/contrib/contenttypes/fields.py +++ b/django/contrib/contenttypes/fields.py @@ -461,7 +461,7 @@ class GenericRelation(ForeignObject): to_opts=opts, target_fields=(opts.pk,), join_field=self, - m2m=not self.unique, + m2m=False, direct=False, filtered_relation=filtered_relation, ) diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py index 6c708fefbba..9b2f21b88b7 100644 --- a/tests/generic_relations_regress/tests.py +++ b/tests/generic_relations_regress/tests.py @@ -308,3 +308,13 @@ class GenericRelationTests(TestCase): thing = HasLinkThing.objects.create() link = Link.objects.create(content_object=thing) self.assertCountEqual(link.targets.all(), [thing]) + + def test_generic_reverse_relation_exclude_filter(self): + place1 = Place.objects.create(name="Test Place 1") + place2 = Place.objects.create(name="Test Place 2") + Link.objects.create(content_object=place1) + link2 = Link.objects.create(content_object=place2) + qs = Link.objects.filter(~Q(places__name="Test Place 1")) + self.assertSequenceEqual(qs, [link2]) + qs = Link.objects.exclude(places__name="Test Place 1") + self.assertSequenceEqual(qs, [link2])