gpt4 book ai didi

python - Django 和 SQLite : DurationField arithmetics

转载 作者:行者123 更新时间:2023-11-28 16:59:42 25 4
gpt4 key购买 nike

我有以下模型

class MyModel(models.Model):
start = models.TimeField()
finish = models.TimeField()
penalty = models.IntegerField(blank=True, default=0) # penalty in minutes

以及管理中的以下查询集注释

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(
total_time=ExpressionWrapper(
F("finish") - F("start") + F("penalty") * 60,
output_field=DurationField()
)
)
return qs

当我检查 penalty != 0 行的注释值时,我得到类似 datetime.timedelta(0, 1800, 1800) 的内容(在本例中为 penalty为 30 分钟)。
但是,我期望像 datetime.timedelta(0, 3600) 这样的时间增量。有人可以解释一下吗?


我也试过

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(
duration=ExpressionWrapper(
F("finish") - F("start"),
output_field=DurationField()
)
)
qs = qs.annotate(
total_time=ExpressionWrapper(
F("duration") + F("route_shortening_penalty") * 60,
output_field=DurationField()
)
)
return qs

甚至

def get_queryset(self, request):
qs = super().get_queryset(request)
qs = qs.annotate(
duration=ExpressionWrapper(
F("finish") - F("start"),
output_field=DurationField()
),
penalty_duration=ExpressionWrapper(
F("penalty") * 60,
output_field=DurationField()
)
)
qs = qs.annotate(
total_time=ExpressionWrapper(
F("duration") + F("penalty_duration"),
output_field=DurationField()
)
)
return qs

两者都导致

Traceback (most recent call last):
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/contrib/admin/options.py", line 604, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1793, in changelist_view
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __len__
self._fetch_all()
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/db/models/query.py", line 63, in __iter__
for row in compiler.results_iter(results):
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1009, in apply_converters
value = converter(value, expression, connection)
File "/home/dm/.virtualenvs/test/lib/python3.6/site-packages/django/db/backends/base/operations.py", line 573, in convert_durationfield_value
return datetime.timedelta(0, 0, value)
TypeError: unsupported type for timedelta microseconds component: str

我正在使用 Python 3.6.7Django==2.1.7SQLite 版本 3022000

最佳答案

如果您得到的是 datetime.timedelta(0, 1800, 1800) 而不是正确的 datetime.timedelta(0, 3600) 那么您显然就差不多了。

我假设 1800 秒是 F("finish")F("start") 之间的差异,而 1800 微秒来自 F(“惩罚”) * 60

乘以 60*1000*1000 而不是 60penalty 从分钟转换为微秒:

total_time=ExpressionWrapper(
F("finish") - F("start") + F("penalty") * 60000000,
output_field=DurationField()
)

关于python - Django 和 SQLite : DurationField arithmetics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com