Refs #25240 -- Added ExtractWeek examples.

This commit is contained in:
Mads Jensen 2017-06-08 10:34:28 +02:00 committed by Tim Graham
parent c16aa69101
commit 085c2f94ec
1 changed files with 10 additions and 7 deletions

View File

@ -438,7 +438,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)
@ -448,12 +448,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
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -482,8 +483,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)
@ -493,15 +494,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