diff options
author | Andriy Sokolovskiy <me@asokolovskiy.com> | 2015-05-09 13:57:13 +0300 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-05-12 19:14:55 -0400 |
commit | f7b297815819153b53dc1125d3f42869fb1b7ebc (patch) | |
tree | 8252263a77561b0887fa5791120154b504338b25 /tests/many_to_many | |
parent | ec74dba2ab3f04fb828c0b926b49f63ec8486dd8 (diff) | |
download | django-f7b297815819153b53dc1125d3f42869fb1b7ebc.tar.gz |
Fixed #24156 -- Fixed inherited related name of ManyToManyField.
Fixed situation when parent abstract model declares related_name='+',
and child models had an invalid queryset.
Diffstat (limited to 'tests/many_to_many')
-rw-r--r-- | tests/many_to_many/models.py | 17 | ||||
-rw-r--r-- | tests/many_to_many/tests.py | 27 |
2 files changed, 43 insertions, 1 deletions
diff --git a/tests/many_to_many/models.py b/tests/many_to_many/models.py index 9e578e4e0b..5688c853d6 100644 --- a/tests/many_to_many/models.py +++ b/tests/many_to_many/models.py @@ -35,3 +35,20 @@ class Article(models.Model): class Meta: ordering = ('headline',) + + +# Models to test correct related_name inheritance +class AbstractArticle(models.Model): + class Meta: + abstract = True + ordering = ('title',) + + publications = models.ManyToManyField(Publication, name='publications', related_name='+') + + +class InheritedArticleA(AbstractArticle): + pass + + +class InheritedArticleB(AbstractArticle): + pass diff --git a/tests/many_to_many/tests.py b/tests/many_to_many/tests.py index 387acb2580..ffe5e879ca 100644 --- a/tests/many_to_many/tests.py +++ b/tests/many_to_many/tests.py @@ -4,7 +4,7 @@ from django.db import transaction from django.test import TestCase from django.utils import six -from .models import Article, Publication +from .models import Article, InheritedArticleA, InheritedArticleB, Publication class ManyToManyTests(TestCase): @@ -454,3 +454,28 @@ class ManyToManyTests(TestCase): self.assertQuerysetEqual(self.a4.publications.all(), []) self.assertQuerysetEqual(self.p2.article_set.all(), ['<Article: NASA finds intelligent life on Earth>']) + + def test_inherited_models_selects(self): + """ + #24156 - Objects from child models where the parent's m2m field uses + related_name='+' should be retrieved correctly. + """ + a = InheritedArticleA.objects.create() + b = InheritedArticleB.objects.create() + a.publications.add(self.p1, self.p2) + self.assertQuerysetEqual(a.publications.all(), + [ + '<Publication: Science News>', + '<Publication: The Python Journal>', + ]) + self.assertQuerysetEqual(b.publications.all(), []) + b.publications.add(self.p3) + self.assertQuerysetEqual(a.publications.all(), + [ + '<Publication: Science News>', + '<Publication: The Python Journal>', + ]) + self.assertQuerysetEqual(b.publications.all(), + [ + '<Publication: Science Weekly>', + ]) |