Skip to content

Commit

Permalink
[2.2.x] Refs #30325 -- Added tests for using count()/exists() with cu…
Browse files Browse the repository at this point in the history
…stom managers and reverse M2M relations.

Backport of 9ac8520 from master
  • Loading branch information
rixx authored and felixxm committed Apr 15, 2019
1 parent e8de1cc commit 5289d4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/many_to_many/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ def __str__(self):
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
# correctly created. Refs #20207
publications = models.ManyToManyField(Publication, name='publications')
tags = models.ManyToManyField(Tag, related_name='tags')

objects = NoDeletedArticleManager()

class Meta:
ordering = ('headline',)

Expand Down
6 changes: 6 additions & 0 deletions tests/many_to_many/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,9 @@ def test_inherited_models_selects(self):
]
)
self.assertQuerysetEqual(b.publications.all(), ['<Publication: Science Weekly>'])

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())

0 comments on commit 5289d4f

Please sign in to comment.