Opened 14 years ago

Closed 9 years ago

Last modified 9 years ago

#8424 closed New feature (fixed)

Implement time filters

Reported by: Vincent Foley Owned by: Ulrich Petri
Component: Database layer (models, ORM) Version:
Severity: Normal Keywords: datetime filter hour minute second
Cc: davidgrant@…, tn@…, renatopedigoni@…, john@…, aurelio@…, dougal85@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


As of r8442, it is possible to filter a queryset by year, month or day using the usual syntax,
However, filters for hours, minutes and seconds were missing. I have added those into a patch for your consideration along with unit tests and documentation.


Attachments (2)

time_filters.diff (4.5 KB) - added by Vincent Foley 14 years ago.
8424-time-filters.diff (9.2 KB) - added by Timothée Peignier 12 years ago.
Updated patch

Download all attachments as: .zip

Change History (24)

Changed 14 years ago by Vincent Foley

Attachment: time_filters.diff added

comment:1 Changed 14 years ago by Thomas Kerpe

Duplicate of #8423 other closed.

comment:2 Changed 14 years ago by Malcolm Tredinnick

milestone: post-1.0
Triage Stage: UnreviewedDesign decision needed

comment:3 Changed 14 years ago by h3

milestone: post-1.01.0 maybe

I can understand why this patch as been scheduled for post-1.0, to respect the API freeze, but it should really be included in the 1.0 release.

Being able to filter by time is not a trivial or candy feature, it *should* be possible, we don't use dateTIME fields just for the fun of it.

Furthermore, in pre-1.0-beta was possible to "hack" a time filter like this:

 def filter_by_hour(obj, time):
     return obj.filter(start_time__contains=' %02d' % time.hour)

Since the beta is out (I don't know the exact revision sorry), it can't be done. It looks like a problem with the database wrapper.. I don't know if it's intentional or not.

Anyway, unless someone can point me out another easy way to filter a datetime field by time, I think this patch is primordial.


comment:4 Changed 14 years ago by Malcolm Tredinnick

milestone: 1.0 maybepost-1.0

Feature freeze has passed. This isn't a bug. It's an enhancement request.

comment:5 Changed 13 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:6 Changed 13 years ago by David Grant

Any chance this could get in to 1.1?

comment:7 Changed 13 years ago by David Grant

Cc: davidgrant@… added

comment:8 Changed 13 years ago by Russell Keith-Magee

Needs tests: set
Patch needs improvement: set
Triage Stage: Design decision neededAccepted
Version: SVN

I've needed these more than once. I can't see any reason to not have them.

Patch needs to be updated; also needs some real tests. Three queries that return nothing doesn't constitute at test of anything.

comment:9 Changed 12 years ago by Ulrich Petri

Owner: changed from nobody to Ulrich Petri

( 2010 sprint)
I'll give this one a shot

comment:10 Changed 12 years ago by tn@…

Cc: tn@… added

comment:11 Changed 12 years ago by renatopedigoni

Cc: renatopedigoni@… added

Changed 12 years ago by Timothée Peignier

Attachment: 8424-time-filters.diff added

Updated patch

comment:12 Changed 12 years ago by Timothée Peignier

I've added an updated patch with more tests (and I'll happily add more if needed).

P.S: Patch is in git diff format.

comment:13 Changed 12 years ago by Ramiro Morales

Last thunk of latest patch

--- a/tests/regressiontests/null_queries/
+++ b/tests/regressiontests/null_queries/
@@ -53,17 +53,17 @@ class NullQueriesTests(TestCase):
         obj = OuterA.objects.create()
-            OuterA.objects.filter(inner__second=None),
+            OuterA.objects.filter(inner__secondary=None),
             ['<OuterA: OuterA object>']
-            OuterA.objects.filter(inner__second__data=None),
+            OuterA.objects.filter(inner__secondary__data=None),
             ['<OuterA: OuterA object>']
         inner_obj = Inner.objects.create(first=obj)
-            Inner.objects.filter(first__inner__second=None),
+            Inner.objects.filter(first__inner__secondary=None),
             ['<Inner: Inner object>']

reveals something potentially dangerous. FTR, from a django-dev IRC channel discussion about it:

cramm: hrm, looking at the patch for #8424.
cramm: Specifically the last thunk. Adding new filtering lookups like 'hour', 'second', ...  means there could be clashes with existing identically named model fields?
cramm: I mean, in user apps
Alex_Gaynor cramm: yeah, it would
Alex_Gaynor: I'd much rather wait for us to have a real solution to alternate filtering, but that won't be 1.4 at the earliest
carljm: cramm, Alex_Gaynor: dunno, I'm inclined to think these are obvious omissions.
carljm: i.e. "wait till we have custom filtering" doesn't make sense to me.
Alex_Gaynor: carljm: unless it breaks everyone's fields named hour, second, minute
carljm: yes. that, of course, is icky.
carljm: that shouldn't be a problem, though. can't the splitter be smarter?
carljm: i.e. only DateTimeFields can have these lookups
carljm: and only related fields can be traversed.
carljm: you can't traverse across a DateTimeField to a related object.
Alex_Gaynor: carljm: it could, ATM it's not
carljm: right. so it's a "patch needs improvement" situation, then.

comment:14 Changed 12 years ago by anonymous

Docs in the patch: "This will match every record with a pub_date that occurred between 3:00 AM and 3:59 AM."

3:59:15 AM is not between 3:00 AM and 3:59 AM but it should be matched.

comment:15 Changed 12 years ago by John Paulett

Cc: john@… added

comment:16 Changed 12 years ago by Aurelio Tinio

Cc: aurelio@… added

comment:17 Changed 11 years ago by Luke Plant

Severity: Normal
Type: New feature

comment:18 Changed 11 years ago by Dougal Matthews

Cc: dougal85@… added
Easy pickings: unset

comment:19 Changed 11 years ago by Ulrich Petri

UI/UX: unset

Related: #16187 Refactor of the lookup system

comment:20 Changed 10 years ago by anonymous

#18049 was a duplicate.

comment:21 Changed 9 years ago by Aymeric Augustin

Resolution: fixed
Status: newclosed

comment:22 Changed 9 years ago by anonymous

why not directly expose __time as well as __hour, __minute, __second?

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