diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py
a
|
b
|
|
50 | 50 | |
51 | 51 | for f in cls._meta.many_to_many: |
52 | 52 | w1 = self.where_class() |
| 53 | db_prep_value = None |
53 | 54 | if isinstance(f, generic.GenericRelation): |
54 | 55 | from django.contrib.contenttypes.models import ContentType |
55 | | field = f.rel.to._meta.get_field(f.content_type_field_name) |
56 | | w1.add((Constraint(None, field.column, field), 'exact', |
| 56 | ct_field = f.rel.to._meta.get_field(f.content_type_field_name) |
| 57 | w1.add((Constraint(None, ct_field.column, ct_field), 'exact', |
57 | 58 | ContentType.objects.get_for_model(cls).id), AND) |
| 59 | id_field = f.rel.to._meta.get_field(f.object_id_field_name) |
| 60 | db_prep_value = id_field.get_db_prep_value |
58 | 61 | for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): |
59 | 62 | where = self.where_class() |
60 | 63 | where.add((Constraint(None, f.m2m_column_name(), f), 'in', |
61 | | pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE]), |
| 64 | map(db_prep_value, |
| 65 | pk_list[offset : offset + GET_ITERATOR_CHUNK_SIZE])), |
62 | 66 | AND) |
63 | 67 | if w1: |
64 | 68 | where.add(w1, AND) |
diff --git a/tests/regressiontests/generic_relations_regress/models.py b/tests/regressiontests/generic_relations_regress/models.py
a
|
b
|
|
40 | 40 | |
41 | 41 | def __unicode__(self): |
42 | 42 | return self.name |
| 43 | |
| 44 | class CharLink(models.Model): |
| 45 | content_type = models.ForeignKey(ContentType) |
| 46 | object_id = models.CharField(max_length=100) |
| 47 | content_object = generic.GenericForeignKey() |
| 48 | |
| 49 | class TextLink(models.Model): |
| 50 | content_type = models.ForeignKey(ContentType) |
| 51 | object_id = models.TextField() |
| 52 | content_object = generic.GenericForeignKey() |
| 53 | |
| 54 | class OddRelation1(models.Model): |
| 55 | name = models.CharField(max_length=100) |
| 56 | clinks = generic.GenericRelation(CharLink) |
| 57 | |
| 58 | class OddRelation2(models.Model): |
| 59 | name = models.CharField(max_length=100) |
| 60 | tlinks = generic.GenericRelation(TextLink) |
| 61 | |
diff --git a/tests/regressiontests/generic_relations_regress/tests.py b/tests/regressiontests/generic_relations_regress/tests.py
a
|
b
|
|
1 | 1 | from django.test import TestCase |
2 | 2 | from django.contrib.contenttypes.models import ContentType |
3 | | from models import Link, Place, Restaurant, Person, Address |
| 3 | from models import Link, Place, Restaurant, Person, Address, CharLink, TextLink, OddRelation1, OddRelation2 |
4 | 4 | |
5 | 5 | class GenericRelationTests(TestCase): |
6 | 6 | |
… |
… |
|
30 | 30 | qs = Person.objects.filter(addresses__zipcode='80433') |
31 | 31 | self.assertEqual(1, qs.count()) |
32 | 32 | self.assertEqual('Chef', qs[0].name) |
| 33 | |
| 34 | def test_charlink_delete(self): |
| 35 | oddrel = OddRelation1.objects.create(name='clink') |
| 36 | cl = CharLink.objects.create(content_object=oddrel) |
| 37 | oddrel.delete() |
| 38 | |
| 39 | def test_textlink_delete(self): |
| 40 | oddrel = OddRelation2.objects.create(name='tlink') |
| 41 | tl = TextLink.objects.create(content_object=oddrel) |
| 42 | oddrel.delete() |