Opened 17 years ago
Closed 17 years ago
#6203 closed (fixed)
.count() not working with DateQuerySet returned from .dates()
Reported by: | 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)
Change History (7)
comment:1 by , 17 years ago
Keywords: | qs-rf added |
---|
comment:2 by , 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.
comment:3 by , 17 years ago
comment:4 by , 17 years ago
Keywords: | qs-rf-fixed added; qs-rf removed |
---|
comment:5 by , 17 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:6 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(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
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?