Opened 17 years ago

Closed 17 years ago

#6203 closed (fixed)

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

Reported by: olau@… Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: qs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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

p.tar.gz (4.4 KB ) - added by Ole Laursen <olau@…> 17 years ago.
Small test project/app

Download all attachments as: .zip

Change History (7)

comment:1 by Malcolm Tredinnick, 17 years ago

Keywords: qs-rf added

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?

comment:2 by Ole Laursen <olau@…>, 17 years ago

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.

by Ole Laursen <olau@…>, 17 years ago

Attachment: p.tar.gz added

Small test project/app

comment:3 by Malcolm Tredinnick, 17 years ago

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

comment:4 by Malcolm Tredinnick, 17 years ago

Keywords: qs-rf-fixed added; qs-rf removed

comment:5 by Malcolm Tredinnick, 17 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Malcolm Tredinnick, 17 years ago

Resolution: fixed
Status: newclosed

(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

Note: See TracTickets for help on using tickets.
Back to Top