python - Django查询ValueError : hour must be in 0. .23

标签 python django

我正在通过 Django 查询集进行查询,它返回“ValueError:小时必须在 0..23 中”过滤日期:

在 models.py 中:

class handoff_model(models.Model):
    batch = models.ForeignKey(batch_def_model, on_delete=models.CASCADE)
    date = models.DateField(blank=True, null=True)

class batch_def_model(models.Model):
    name = models.CharField(max_length=200)
    start_time = models.TimeField(blank=True, null=True)

在views.py中:

def ho_open(request):
    date = '2019-07-29'
    all_b = batch_def_model.objects.all()
    for b in all_b:
        if not handoff_model.objects.filter(date=date, batch=b.name).exists():
            batch = handoff_model(batch=b, date=date)
            batch.save()
    handoff_list = handoff_model.objects.filter(date=date,batch__start_time__lt='08:00')
    return handoff_list

我的数据库中已经有一些“batch_def_model”对象。每次运行“ho_open”(更改硬编码日期)时,它都应该创建与“batch_def_model”相同的 handoff_models,但针对硬编码日期。

当我将日期设置为“2019-07-29”时,它可以正常工作。当日期为“2019-07-30”或“2019-07-31”时,我收到以下错误:

完整的跟踪是:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 248, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1068, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
    self._post_get_result()
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 416, in _post_get_result
    self._rows = self._fetch_row(0)
  File "/usr/local/lib/python3.6/site-packages/MySQLdb/cursors.py", line 384, in _fetch_row
    return self._result.fetch_row(size, self._fetch_type)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 151, in typecast_time
    return datetime.time(int(hour), int(minutes), int(seconds), int((microseconds + '000000')[:6]))
ValueError: hour must be in 0..23

我不确定为什么它不适用于这些日期,因为整个月都工作正常。我已经通过使用 date__contains=date 进行过滤进行了测试,但没有成功。

已编辑:我意识到行已正确记录。问题是在尝试选择这一行时:

handoff_list = handoff_model.objects.filter(date=date,batch__start_time__lt='08:00')

你有什么提示吗?

最佳答案

你的views.py变化不大。这将是我的第一个开始猜测。由于您的 start_time 是一个 TimeField 实例(根据 documentation 是一个 datetime.time 实例。因此,batch__start_time__lt 运算符的右侧也应该是一个 datetime.time 实例。

import datetime
def ho_open(request):
    date = '2019-07-29'
    all_b = batch_def_model.objects.all()
    for b in all_b:
        if not handoff_model.objects.filter(date=date, batch=b.name).exists():
            batch = handoff_model(batch=b, date=date)
            batch.save()
    handoff_list = handoff_model.objects.filter(date=date,batch__start_time__lt=datetime.time(8,0))
    return handoff_list

这是我的猜测,没有机会查看代码中的其他地方。我测试它是否有效的方法有限。如果有错误,请告诉我。

关于python - Django查询ValueError : hour must be in 0. .23,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57293500/

相关文章:

python - 如何在 PythonMagick 中处理多页图像?

django ckeditor 图片上传

python - Django URLconf : How to use captured params in include's RedirectView?

django - 如何在 Django 中正确处理中止的 HTTP 请求

python - 如何改进这个python矢量函数

python - 变量交替是否存在于 Python 调试器中

python - PyCharm 无缘无故运行测试时抛出 "AttributeError: ' 模块“对象没有属性”

python - 处理 Celery 任务代码中的错误的推荐方法是什么?

python - 分离进度跟踪和循环逻辑

python - Django:加载数据不工作