Opened 9 years ago

Last modified 8 years ago

#25774 closed New feature

Refactor of datetime expressions and better, official support for right-hand-side date part extraction — at Version 1

Reported by: David Filipovic Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: db, expressions, date, time, extract, transform 1.10
Cc: josh.smeaton@…, info+coding@…, aksheshdoshi@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by David Filipovic)

As previously stated in this ticket and this thread, I have begun work on the following:

  • making the so called datetime transforms (currently residing in django.db.models.lookups) part of the officially supported ORM API as part of the db expressions,
  • making these transforms easily usable on the right hand side of lookups / Q objects


To this effect, I propose the following:

  • replace module django.db.models.expressions with a package of the same name
  • add module django.db.models.expressions.datetimes
  • rename XTransform to XExtract and move them from django.db.models.lookups to django.db.models.lookups to django.db.models.expressions.datetimes
  • change YearLookup and YearComparisonLookup to allow for functions on the right hand side
  • extensive testing of these db expressions
  • adding this to the release notes
  • any other change that might support this


I'm also adding a PR with some of these already implemented. What remains to be done is:

  • replace references XTransform type classes in django.db.models.lookups with XExtract classes from django.db.models.expressions.datetimes
  • replace these references anywhere else in the project (I've noticed there are some in the tests, for instance)


With the API in the PR it becomes possible to do the following lookup:

from django.db.models.expressions.datetimes import DateExtract

Person.objects.filter(birth_date__year=DateExtract('job__start_date', lookup_name='year'))

which is equivalent to:

from django.db.models.expressions.datetimes import YearExtract

Person.objects.filter(birth_date__year=YearExtract('job__start_date'))

Additionally, @jarshwah suggested that these should maybe be named: ExtractX instead of XExtract.

Change History (1)

comment:1 by David Filipovic, 9 years ago

Description: modified (diff)
Needs documentation: set
Needs tests: set
Note: See TracTickets for help on using tickets.
Back to Top