Django

Code

Ticket #6203 (closed: fixed)

Opened 5 months ago

Last modified 3 weeks ago

.count() not working with DateQuerySet returned from .dates()

Reported by: olau@iola.dk Assigned to: nobody
Component: Database wrapper Version: SVN
Keywords: qs-rf-fixed Cc:
Triage Stage: Accepted Has patch: 0
Needs documentation: 0 Needs tests: 0
Patch needs improvement: 0

Description

The problem is the following:

Readout.objects.dates('timestamp', 'day').count() -> returns 2712 len(Readout.objects.dates('timestamp', 'day')) -> returns 12

I've been using len(), but then noticed that the documentation for count() suggests using count() instead for performance reasons. I didn't expect to see different results, though. :-)

Attachments

p.tar.gz (4.4 kB) - added by Ole Laursen <olau@iola.dk> on 12/17/07 06:07:16.
Small test project/app

Change History

12/17/07 04:26:25 changed by mtredinnick

  • keywords set to qs-rf.
  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

What sort of data are you using to replicate this problem? Before I go digging too deeply, it would be nice to know how difficult it is to replicate. Are the timestamps all on the same day? Or spread over multiple days?

12/17/07 06:06:41 changed by Ole Laursen <olau@iola.dk>

My data is spread over several days. 'timestamp' is a datetime field. It's easy to replicate, I looked in the class that dates "returns" and it didn't appear to have a count at all. Here's a simple test class:

class TestModel(models.Model):
    timestamp = models.DateTimeField()

I started a project, filled in settings.py with an SQLite database and added an app with this model. Here's the output from a session with "manage.py shell":

>>> from s.models import TestModel
>>> t = TestModel()
>>> from datetime import datetime
>>> t.timestamp = datetime.now()
>>> t.save()
>>> t = TestModel()
>>> from datetime import timedelta
>>> t.timestamp = datetime.now() - timedelta(1)
>>> t.save()
>>> t = TestModel()
>>> t.timestamp = datetime.now() - timedelta(1, 1)
>>> t.save()
>>> TestModel.objects.all()
[<TestModel: TestModel object>, <TestModel: TestModel object>, <TestModel: TestModel object>]
>>> TestModel.objects.count()
3
>>> TestModel.objects.dates('timestamp', 'day').count()
3
>>> TestModel.objects.dates('timestamp', 'day')
[datetime.datetime(2007, 12, 16, 0, 0), datetime.datetime(2007, 12, 17, 0, 0)]
>>> len(TestModel.objects.dates('timestamp', 'day'))
2

I'll attach the project as a tarball. It's already got the three objects created in the snippet above, so you should be able to reproduce the bug with just:

from s.models import TestModel
TestModel.objects.dates('timestamp', 'day').count()
len(TestModel.objects.dates('timestamp', 'day'))

Hope this helps.

12/17/07 06:07:16 changed by Ole Laursen <olau@iola.dk>

  • attachment p.tar.gz added.

Small test project/app

12/19/07 06:23:59 changed by mtredinnick

(In [6959]) queryset-refactor: Made qs.dates(...).count() work.

This involved a slight change in the SQL for .dates() which appears to be correct and passes all the tests, but may have some side-effect I don't know about.

Refs #6203.

12/19/07 06:29:56 changed by mtredinnick

  • keywords changed from qs-rf to qs-rf-fixed.

12/19/07 06:30:02 changed by mtredinnick

  • stage changed from Unreviewed to Accepted.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658


Add/Change #6203 (.count() not working with DateQuerySet returned from .dates())




Change Properties
Action