[1.11.x] Refs #25240 -- Added ExtractWeek examples.

Backport of 085c2f94ec from master
This commit is contained in:
Mads Jensen 2017-06-08 10:34:28 +02:00 committed by Tim Graham
parent 288fd9b9e0
commit 319839d780
1 changed files with 10 additions and 7 deletions

View File

@ -407,7 +407,7 @@ that deal with date-parts can be used with ``DateField``::
>>> from datetime import datetime >>> from datetime import datetime
>>> from django.utils import timezone >>> from django.utils import timezone
>>> from django.db.models.functions import ( >>> from django.db.models.functions import (
... ExtractYear, ExtractMonth, ExtractDay, ExtractWeekDay ... ExtractDay, ExtractMonth, ExtractWeek, ExtractWeekDay, ExtractYear,
... ) ... )
>>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc) >>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc)
>>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc) >>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc)
@ -417,12 +417,13 @@ that deal with date-parts can be used with ``DateField``::
>>> Experiment.objects.annotate( >>> Experiment.objects.annotate(
... year=ExtractYear('start_date'), ... year=ExtractYear('start_date'),
... month=ExtractMonth('start_date'), ... month=ExtractMonth('start_date'),
... week=ExtractWeek('start_date'),
... day=ExtractDay('start_date'), ... day=ExtractDay('start_date'),
... weekday=ExtractWeekDay('start_date'), ... weekday=ExtractWeekDay('start_date'),
... ).values('year', 'month', 'day', 'weekday').get( ... ).values('year', 'month', 'week', 'day', 'weekday').get(
... end_date__year=ExtractYear('start_date'), ... end_date__year=ExtractYear('start_date'),
... ) ... )
{'year': 2015, 'month': 6, 'day': 15, 'weekday': 2} {'year': 2015, 'month': 6, 'week': 25, 'day': 15, 'weekday': 2}
``DateTimeField`` extracts ``DateTimeField`` extracts
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -451,8 +452,8 @@ Each class is also a ``Transform`` registered on ``DateTimeField`` as
>>> from datetime import datetime >>> from datetime import datetime
>>> from django.utils import timezone >>> from django.utils import timezone
>>> from django.db.models.functions import ( >>> from django.db.models.functions import (
... ExtractYear, ExtractMonth, ExtractDay, ExtractWeekDay, ... ExtractDay, ExtractHour, ExtractMinute, ExtractMonth, ExtractSecond,
... ExtractHour, ExtractMinute, ExtractSecond, ... ExtractWeek, ExtractWeekDay, ExtractYear,
... ) ... )
>>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc) >>> start_2015 = datetime(2015, 6, 15, 23, 30, 1, tzinfo=timezone.utc)
>>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc) >>> end_2015 = datetime(2015, 6, 16, 13, 11, 27, tzinfo=timezone.utc)
@ -462,15 +463,17 @@ Each class is also a ``Transform`` registered on ``DateTimeField`` as
>>> Experiment.objects.annotate( >>> Experiment.objects.annotate(
... year=ExtractYear('start_datetime'), ... year=ExtractYear('start_datetime'),
... month=ExtractMonth('start_datetime'), ... month=ExtractMonth('start_datetime'),
... week=ExtractWeek('start_datetime'),
... day=ExtractDay('start_datetime'), ... day=ExtractDay('start_datetime'),
... weekday=ExtractWeekDay('start_datetime'), ... weekday=ExtractWeekDay('start_datetime'),
... hour=ExtractHour('start_datetime'), ... hour=ExtractHour('start_datetime'),
... minute=ExtractMinute('start_datetime'), ... minute=ExtractMinute('start_datetime'),
... second=ExtractSecond('start_datetime'), ... second=ExtractSecond('start_datetime'),
... ).values( ... ).values(
... 'year', 'month', 'day', 'weekday', 'hour', 'minute', 'second', ... 'year', 'month', 'week', 'day', 'weekday', 'hour', 'minute', 'second',
... ).get(end_datetime__year=ExtractYear('start_datetime')) ... ).get(end_datetime__year=ExtractYear('start_datetime'))
{'year': 2015, 'month': 6, 'day': 15, 'weekday': 2, 'hour': 23, 'minute': 30, 'second': 1} {'year': 2015, 'month': 6, 'week': 25, 'day': 15, 'weekday': 2, 'hour': 23,
'minute': 30, 'second': 1}
When :setting:`USE_TZ` is ``True`` then datetimes are stored in the database When :setting:`USE_TZ` is ``True`` then datetimes are stored in the database
in UTC. If a different timezone is active in Django, the datetime is converted in UTC. If a different timezone is active in Django, the datetime is converted