﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30325	Inconsistent count() behaviour on reverse relations in ManyToManyFields with custom model manager	Tobias Kunze	Tobias Kunze	"When looking up a reverse relation on a ManyToManyField that uses a custom model manager, not using `all()` before calling `count()` will not use the custom model manager. This results in `obj.m2m_lookup.count()` yielding a different result than `obj.m2m_lookup.all().count`, which is new behaviour in 2.2, and unexpected (at least to me).

I've written up a [https://github.com/rixx/django-22-issue minimal test case] to showcase the issue. Inlining here:

{{{
#!python
from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=100)


class BookManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().exclude(state='deleted')


class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(to=Author, related_name='books')
    state = models.CharField(choices=(('regular', 'regular'), ('deleted', 'deleted')), default='regular', max_length=100)

    objects = BookManager()

a = Author.objects.create(name='Bill')
b1 = Book.objects.create(title='Sonnets', state='deleted')
b2 = Book.objects.create(title='Hamlet')
b1.authors.add(a)
b2.authors.add(a)
a.books.count()  # Returns 2
a.books.all().count()  # Returns 1
}}}"	Bug	closed	Database layer (models, ORM)	2.2	Release blocker	fixed			Ready for checkin	1	0	0	0	0	0
