Opened 8 years ago

Closed 3 years ago

Last modified 2 years ago

#8424 closed New feature (fixed)

Implement time filters

Reported by: gnuvince Owned by: UloPe
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 gnuvince 8 years ago.
8424-time-filters.diff (9.2 KB) - added by cyberdelia 6 years ago.
Updated patch

Download all attachments as: .zip

Change History (24)

Changed 8 years ago by gnuvince

comment:1 Changed 8 years ago by toke

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Duplicate of #8423 other closed.

comment:2 Changed 8 years ago by mtredinnick

  • milestone set to post-1.0
  • Triage Stage changed from Unreviewed to Design decision needed

comment:3 Changed 8 years ago by h3

  • milestone changed from post-1.0 to 1.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 8 years ago by mtredinnick

  • milestone changed from 1.0 maybe to post-1.0

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

comment:5 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:6 Changed 7 years ago by dgrant

Any chance this could get in to 1.1?

comment:7 Changed 7 years ago by dgrant

  • Cc davidgrant@… added

comment:8 Changed 6 years ago by russellm

  • Needs tests set
  • Patch needs improvement set
  • Triage Stage changed from Design decision needed to Accepted
  • Version SVN deleted

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 6 years ago by UloPe

  • Owner changed from nobody to UloPe

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

comment:10 Changed 6 years ago by tn@…

  • Cc tn@… added

comment:11 Changed 6 years ago by renatopedigoni

  • Cc renatopedigoni@… added

Changed 6 years ago by cyberdelia

Updated patch

comment:12 Changed 6 years ago by cyberdelia

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 6 years ago by ramiro

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 6 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 5 years ago by jpaulett

  • Cc john@… added

comment:16 Changed 5 years ago by aurelio

  • Cc aurelio@… added

comment:17 Changed 5 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:18 Changed 5 years ago by d0ugal

  • Cc dougal85@… added
  • Easy pickings unset

comment:19 Changed 5 years ago by UloPe

  • UI/UX unset

Related: #16187 Refactor of the lookup system

comment:20 Changed 4 years ago by anonymous

#18049 was a duplicate.

comment:21 Changed 3 years ago by aaugustin

  • Resolution set to fixed
  • Status changed from new to closed

comment:22 Changed 2 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