From 9ac8520fcde29840a1345be19d80dbda53aa6d03 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Mon, 15 Apr 2019 11:03:53 +0200 Subject: [PATCH] Refs #30325 -- Added tests for using count()/exists() with custom managers and reverse M2M relations. --- tests/many_to_many/models.py | 7 +++++++ tests/many_to_many/tests.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/tests/many_to_many/models.py b/tests/many_to_many/models.py index 28d23f2a82..7b46d2484e 100644 --- a/tests/many_to_many/models.py +++ b/tests/many_to_many/models.py @@ -27,6 +27,11 @@ class Tag(models.Model): return self.name +class NoDeletedArticleManager(models.Manager): + def get_queryset(self): + return super().get_queryset().exclude(headline='deleted') + + class Article(models.Model): headline = models.CharField(max_length=100) # Assign a string as name to make sure the intermediary model is @@ -34,6 +39,8 @@ class Article(models.Model): publications = models.ManyToManyField(Publication, name='publications') tags = models.ManyToManyField(Tag, related_name='tags') + objects = NoDeletedArticleManager() + class Meta: ordering = ('headline',) diff --git a/tests/many_to_many/tests.py b/tests/many_to_many/tests.py index 9dc53307a1..7434d18e02 100644 --- a/tests/many_to_many/tests.py +++ b/tests/many_to_many/tests.py @@ -580,3 +580,9 @@ class ManyToManyTests(TestCase): ] ) self.assertQuerysetEqual(b.publications.all(), ['']) + + def test_custom_default_manager_exists_count(self): + a5 = Article.objects.create(headline='deleted') + a5.publications.add(self.p2) + self.assertEqual(self.p2.article_set.count(), self.p2.article_set.all().count()) + self.assertEqual(self.p3.article_set.exists(), self.p3.article_set.all().exists())