summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroman_p <romanover9000@gmail.com>2022-11-17 17:54:30 +0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-11-18 10:13:31 +0100
commit04b15022e8d1f49af69d8a1e6cd678f31f1280ff (patch)
treed660397856e96a6f779568f8993ab6a2f9c9b795
parent51faf4bd172cd4cb219a9793facbfa00246c9f3c (diff)
Fixed #26261 -- Fixed queryset crash when excluding reverse GenericRelation.
Thanks Amir Hadi for the report.
-rw-r--r--django/contrib/contenttypes/fields.py2
-rw-r--r--tests/generic_relations_regress/tests.py10
2 files changed, 11 insertions, 1 deletions
diff --git a/django/contrib/contenttypes/fields.py b/django/contrib/contenttypes/fields.py
index 72b9e7631a..35fcd0d908 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 6c708fefbb..9b2f21b88b 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])