#30325: Inconsistent count() behaviour on reverse relations in ManyToManyFields
with custom model manager
-------------------------------------+-------------------------------------
               Reporter:  Tobias     |          Owner:  nobody
  Kunze                              |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  2.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30325>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/047.7c653ce8d7d4ac9e30ddc1b844aa0f4a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to