根据此处的Django文档:https://docs.djangoproject.com/en/2.1/ref/models/expressions/#using-f-with-annotations
我可以使用ExpressionWrapper进行日期计算。我试图这样使用它:
sprints = Sprint.objects.annotate(
duration=models.Case(
models.When(
Q(started__isnull=False) &
Q(done__isnull=False),
then=models.Value(
models.ExpressionWrapper(
(models.F('done') - models.F('started')),
output_field=models.DateTimeField()
))
),
models.When(
Q(started__isnull=False) &
Q(done__isnull=True),
then=models.Value(
models.ExpressionWrapper(
(Now() - models.F('started')),
output_field=models.DateTimeField(),
))
),
output_field=models.DateTimeField()
)).values_list('name',
'planned',
'started',
'done',
'duration')
但是从pytz尝试在ExpressionWrapper上进行自动定位时出现错误:
Traceback (most recent call last):
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
response = get_response(request)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/rest_framework/viewsets.py", line 103, in view
return self.dispatch(request, *args, **kwargs)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/home/phirt/src/core3/src/backend/portal/views/api/transformation.py", line 766, in get_sprint_data_csv
for sprint in sprints:
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
self._fetch_all()
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1011, in apply_converters
value = converter(value, expression, connection)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 247, in convert_datetimefield_value
value = timezone.make_aware(value, self.connection.timezone)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/django/utils/timezone.py", line 269, in make_aware
return timezone.localize(value, is_dst=is_dst)
File "/home/phirt/local/venv/core3/lib/python3.6/site-packages/pytz/__init__.py", line 226, in localize
if dt.tzinfo is not None:
AttributeError: 'str' object has no attribute 'tzinfo'
实际上,以pytz传递给dt
的{{1}}参数是一个字符串,它正是我的ExpressionWrapper表达式:localize
我该如何做?
编辑:这是sprint模型:
ExpressionWrapper(F(done) - F(started))
答案 0 :(得分:1)
我通过将查询注释更改为以下内容来使其工作:
B.temptable