Opened 7 years ago
Last modified 6 years ago
#28649 closed New feature
Add "week_year" lookup to DateField/DateTimeField — at Version 4
Reported by: | Sigurd Ljødal | Owned by: | Sigurd Ljødal |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | ORM Extract |
Cc: | Mariusz Felisiak | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I found myself in the need to extract the ISO year/week-numbering year (as specified by ISO 8601) from a date/datetime field, e.g. to group values by week. This is easy with PostgreSQL:
from django.db.models.functions import Extract, ExtractWeek SomeModel.objects.annotate( year=Extract('some_date', 'isoyear'), week=ExtractWeek('some_date'), ).values( 'year', 'week', ).annotate( sum=Sum('some_field'), )
but unfortunately extracting the week year like this does not work across databases. I've implemented an ExtractWeekYear
class with support for all databases and will submit a pull request shortly.
Change History (4)
comment:1 by , 7 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
comment:2 by , 7 years ago
As you mentioned there already exists ExtractWeek
and week
lookup (see #25240) and it works properly on all internal backends. Please clarify "this does not work across databases".
comment:3 by , 7 years ago
Cc: | added |
---|
comment:4 by , 7 years ago
Description: | modified (diff) |
---|
Hey felixxm, looks like you've misunderstood what I have implemented, it's not the ExtractWeek
class, but a cross database solution for Extract('some_date', 'isoyear')
. I've updated the description a bit to clarify this.
The week-numbering year is different from the Julian year, as e.g. January 1st, 2017 is in week 52 of 2016.
PR