summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnssi Kääriäinen <akaariai@gmail.com>2013-05-20 17:40:34 +0300
committerAnssi Kääriäinen <akaariai@gmail.com>2013-05-20 18:02:10 +0300
commitc9a96075fa02b6d52bec748ffdfb413688a15774 (patch)
tree5d61b70044a48ac8d916a5712d4a64fb751128f6
parentd9d24c4521866247f1e5fc1150f5cb9b57979863 (diff)
downloaddjango-c9a96075fa02b6d52bec748ffdfb413688a15774.tar.gz
Fixed #20378 -- regression in GenericRelation on abstract model
When a GenericRelation was defined on abstract model, queries on childs of the abstract model didn't work. The problem was in the way fields and in particular field.rel was copied from models to their children. The regression was likely caused by #19385. Thanks to Gavin Wahl for spotting the regression.
-rw-r--r--django/db/models/fields/__init__.py2
-rw-r--r--tests/generic_relations_regress/models.py9
-rw-r--r--tests/generic_relations_regress/tests.py20
3 files changed, 30 insertions, 1 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 13cec1bfba..7d01391d4f 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -157,6 +157,8 @@ class Field(object):
obj = copy.copy(self)
if self.rel:
obj.rel = copy.copy(self.rel)
+ if hasattr(self.rel, 'field') and self.rel.field is self:
+ obj.rel.field = obj
memodict[id(self)] = obj
return obj
diff --git a/tests/generic_relations_regress/models.py b/tests/generic_relations_regress/models.py
index 2795471f7f..206dbe02b0 100644
--- a/tests/generic_relations_regress/models.py
+++ b/tests/generic_relations_regress/models.py
@@ -122,3 +122,12 @@ class Tag(models.Model):
class Board(models.Model):
name = models.CharField(primary_key=True, max_length=15)
+
+class HasLinks(models.Model):
+ links = generic.GenericRelation(Link)
+
+ class Meta:
+ abstract = True
+
+class HasLinkThing(HasLinks):
+ pass
diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py
index 474113584a..519ca203a9 100644
--- a/tests/generic_relations_regress/tests.py
+++ b/tests/generic_relations_regress/tests.py
@@ -4,7 +4,7 @@ from django.test import TestCase, skipIfDBFeature
from .models import (Address, Place, Restaurant, Link, CharLink, TextLink,
Person, Contact, Note, Organization, OddRelation1, OddRelation2, Company,
- Developer, Team, Guild, Tag, Board)
+ Developer, Team, Guild, Tag, Board, HasLinkThing)
class GenericRelationTests(TestCase):
@@ -135,3 +135,21 @@ class GenericRelationTests(TestCase):
b1 = Board.objects.create(name='')
tag = Tag(label='VP', content_object=b1)
tag.save()
+
+ def test_ticket_20378(self):
+ hs1 = HasLinkThing.objects.create()
+ hs2 = HasLinkThing.objects.create()
+ l1 = Link.objects.create(content_object=hs1)
+ l2 = Link.objects.create(content_object=hs2)
+ self.assertQuerysetEqual(
+ HasLinkThing.objects.filter(links=l1),
+ [hs1], lambda x: x)
+ self.assertQuerysetEqual(
+ HasLinkThing.objects.filter(links=l2),
+ [hs2], lambda x: x)
+ self.assertQuerysetEqual(
+ HasLinkThing.objects.exclude(links=l2),
+ [hs1], lambda x: x)
+ self.assertQuerysetEqual(
+ HasLinkThing.objects.exclude(links=l1),
+ [hs2], lambda x: x)