#30655: len(queryset) and queryset.count() return different results
----------------------------------+--------------------------------------
     Reporter:  Sander Kleijwegt  |                    Owner:  nobody
         Type:  Bug               |                   Status:  new
    Component:  Uncategorized     |                  Version:  2.2
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Changes (by Sander Kleijwegt):

 * version:  2.0 => 2.2


Old description:

> According to the queryset.count() documentation "you should always use
> count() rather than loading all of the record into Python objects and
> calling len() on the result" (see
> [https://docs.djangoproject.com/en/2.0/ref/models/querysets/#count
> here]), which seems to imply that the results of both functions should
> always be identical. Here is a simple example that shows the contrary.
>
> Models:
> {{{
> class Bar(models.Model):
>         pass
>
> class Foo(models.Model):
>         fooint = models.IntegerField()
>         foobar = models.ForeignKey(Bar, related_name='foos',
> on_delete=models.CASCADE)
> }}}
>
> Shell Output:
> {{{
> >>> import django
> >>> django.__version__
> '2.0.4'
> >>> from foobar.models import Foo, Bar
> >>> bar = Bar.objects.create()
> >>> foo1 = Foo.objects.create(fooint=1, foobar=bar)
> >>> foo2 = Foo.objects.create(fooint=2, foobar=bar)
> >>> Bar.objects.all().order_by('foos__fooint').count()
> 1
> >>> len(Bar.objects.all().order_by('foos__fooint'))
> 2
> >>> Bar.objects.all().order_by('foos__fooint')
> <QuerySet [<Bar: Bar object (1)>, <Bar: Bar object (1)>]>
> }}}

New description:

 According to the queryset.count() documentation "you should always use
 count() rather than loading all of the record into Python objects and
 calling len() on the result" (see
 [https://docs.djangoproject.com/en/2.0/ref/models/querysets/#count here]),
 which seems to imply that the results of both functions should always be
 identical. Here is a simple example that shows the contrary.

 Models:
 {{{
 class Bar(models.Model):
         pass

 class Foo(models.Model):
         fooint = models.IntegerField()
         foobar = models.ForeignKey(Bar, related_name='foos',
 on_delete=models.CASCADE)
 }}}

 Shell Output:
 {{{
 >>> import django
 >>> django.__version__
 '2.2.3'
 >>> from foobar.models import Foo, Bar
 >>> bar = Bar.objects.create()
 >>> foo1 = Foo.objects.create(fooint=1, foobar=bar)
 >>> foo2 = Foo.objects.create(fooint=2, foobar=bar)
 >>> Bar.objects.all().order_by('foos__fooint').count()
 1
 >>> len(Bar.objects.all().order_by('foos__fooint'))
 2
 >>> Bar.objects.all().order_by('foos__fooint')
 <QuerySet [<Bar: Bar object (1)>, <Bar: Bar object (1)>]>
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30655#comment:1>
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.b15b3a0192f1259f44f661801e39277a%40djangoproject.com.

Reply via email to