我正在通过 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')
你有什么提示吗?
你的观点变化不大。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
这是我的猜测,没有机会查看代码中的其他地方。我测试它是否有效的方法有限。如果有错误请告诉我。
这通常是由数据输入错误引起的(主要是用户造成的)。
在我的例子中,为时间字段输入了 24:00:00 的值,但 python 无法识别 24:00:00,python 期望 00:00:00 作为午夜的值。