Code

Ticket #12721: 12721_r12344.diff

File 12721_r12344.diff, 3.4 KB (added by carljm, 4 years ago)
Line 
1diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
2--- a/django/db/models/sql/subqueries.py
3+++ b/django/db/models/sql/subqueries.py
4@@ -50,15 +50,19 @@
5 
6         for f in cls._meta.many_to_many:
7             w1 = self.where_class()
8+            db_prep_value = None
9             if isinstance(f, generic.GenericRelation):
10                 from django.contrib.contenttypes.models import ContentType
11-                field = f.rel.to._meta.get_field(f.content_type_field_name)
12-                w1.add((Constraint(None, field.column, field), 'exact',
13+                ct_field = f.rel.to._meta.get_field(f.content_type_field_name)
14+                w1.add((Constraint(None, ct_field.column, ct_field), 'exact',
15                         ContentType.objects.get_for_model(cls).id), AND)
16+                id_field = f.rel.to._meta.get_field(f.object_id_field_name)
17+                db_prep_value = id_field.get_db_prep_value
18             for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
19                 where = self.where_class()
20                 where.add((Constraint(None, f.m2m_column_name(), f), 'in',
21-                        pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]),
22+                        map(db_prep_value,
23+                            pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE])),
24                         AND)
25                 if w1:
26                     where.add(w1, AND)
27diff --git a/tests/regressiontests/generic_relations_regress/models.py b/tests/regressiontests/generic_relations_regress/models.py
28--- a/tests/regressiontests/generic_relations_regress/models.py
29+++ b/tests/regressiontests/generic_relations_regress/models.py
30@@ -40,3 +40,22 @@
31 
32     def __unicode__(self):
33         return self.name
34+
35+class CharLink(models.Model):
36+    content_type = models.ForeignKey(ContentType)
37+    object_id = models.CharField(max_length=100)
38+    content_object = generic.GenericForeignKey()
39+
40+class TextLink(models.Model):
41+    content_type = models.ForeignKey(ContentType)
42+    object_id = models.TextField()
43+    content_object = generic.GenericForeignKey()
44+
45+class OddRelation1(models.Model):
46+    name = models.CharField(max_length=100)
47+    clinks = generic.GenericRelation(CharLink)
48+
49+class OddRelation2(models.Model):
50+    name = models.CharField(max_length=100)
51+    tlinks = generic.GenericRelation(TextLink)
52+
53diff --git a/tests/regressiontests/generic_relations_regress/tests.py b/tests/regressiontests/generic_relations_regress/tests.py
54--- a/tests/regressiontests/generic_relations_regress/tests.py
55+++ b/tests/regressiontests/generic_relations_regress/tests.py
56@@ -1,6 +1,6 @@
57 from django.test import TestCase
58 from django.contrib.contenttypes.models import ContentType
59-from models import Link, Place, Restaurant, Person, Address
60+from models import Link, Place, Restaurant, Person, Address, CharLink, TextLink, OddRelation1, OddRelation2
61 
62 class GenericRelationTests(TestCase):
63 
64@@ -30,3 +30,13 @@
65         qs = Person.objects.filter(addresses__zipcode='80433')
66         self.assertEqual(1, qs.count())
67         self.assertEqual('Chef', qs[0].name)
68+
69+    def test_charlink_delete(self):
70+        oddrel = OddRelation1.objects.create(name='clink')
71+        cl = CharLink.objects.create(content_object=oddrel)
72+        oddrel.delete()
73+
74+    def test_textlink_delete(self):
75+        oddrel = OddRelation2.objects.create(name='tlink')
76+        tl = TextLink.objects.create(content_object=oddrel)
77+        oddrel.delete()