Opened 10 years ago
Last modified 9 years ago
#25774 closed New feature
Refactor of datetime expressions and better, official support for right-hand-side date part extraction — at Initial Version
| 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
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.expressionswith a package of the same name - add module
django.db.models.expressions.datetimes - rename
XTransformtoXExtractand move them fromdjango.db.models.lookupstodjango.db.models.lookupstodjango.db.models.expressions.datetimes - change
YearLookupandYearComparisonLookupto 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
XTransformtype classes indjango.db.models.lookupswithXExtractclasses fromdjango.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.
Note:
See TracTickets
for help on using tickets.