Opened 8 years ago
Last modified 7 years ago
#28649 closed New feature
Add "week_year" lookup to DateField/DateTimeField — at Initial Version
| Reported by: | Sigurd Ljødal | Owned by: | nobody |
|---|---|---|---|
| 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
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 this does not work across databases. I've implemented an ExtractWeekYear class with support for all databases and will submit a pull request shortly.
Note:
See TracTickets
for help on using tickets.