【问题标题】:combine() argument 1 must be datetime.date, not strcombine() 参数 1 必须是 datetime.date,而不是 str
【发布时间】:2020-04-02 19:38:29
【问题描述】:

在 Django 日期范围中使用查询集时出现错误

from_date = request.POST.get('from_date')
to_date = request.POST.get('to_date')
min_dt = datetime.datetime.combine(from_date, datetime.time.min)
max_dt = datetime.datetime.combine(to_date, datetime.time.max)
daily_en = All_enquiries.objects.filter(enquired_at__range = (min_dt, max_dt))

当手动使用变量时,它的工作正常如下

from_date = datetime.date(2019, 12, 9)
to_date = datetime.date(2019, 12, 9)

【问题讨论】:

  • 这是因为来自request.POST 的数据是string 类型。您需要将其转换为datetime 对象

标签: python django django-queryset


【解决方案1】:

我猜如果你将 request.POST.get() 返回的字符串转换为 datetime.date 它应该可以工作,但我不知道 request.POST.get() 的内容

【讨论】:

  • 使用其中一些示例:stackabuse.com/converting-strings-to-datetime-in-python 取决于字符串的日期格式
  • 我得到输入类型=日期格式请评论如何转换为日期时间
  • 添加这行代码: print(from_date) print(to_date) 看哪种格式返回POST请求,没有这个信息我不知道怎么转换
猜你喜欢
  • 2015-04-24
  • 2018-09-04
  • 2017-10-21
  • 1970-01-01
  • 2020-07-11
  • 2019-03-15
  • 1970-01-01
  • 2019-10-29
  • 2022-11-21
相关资源
最近更新 更多