[英]“An integer is required” datetime field
what i wanted to do with this code is get all the datas thats dated older than yesterday. 我要用此代码执行的操作是获取所有日期早于昨天的数据。 It throws an error "an integer is required" at the line:
它在该行上引发错误“需要整数”:
date = datetime.date(year, month, yesterday)
日期= datetime.date(年,月,昨天)
So far to my knowledge, its taking year as an integer but not month field. 据我所知,它以年份为整数,而不是月份字段。 It takes the month field as the default datetime field.
它以月字段作为默认日期时间字段。
Heres my view: 这是我的观点:
current = datetime.datetime.now()
yesterday = datetime.datetime.today() + datetime.timedelta(days = -1)
year = datetime.date.today().year
month = datetime.date.today() + relativedelta(months = -1)
date = datetime.date(year, month, yesterday)
hist_obj = Events.objects.filter(uploader = request.user,
start_date__lte = date)
return render_to_response('history.html', {'history_obj':hist_obj})
This code is confusing. 这段代码令人困惑。
yesterday
and month
are both datetimes, because that's how you defined them in lines 2 and 4. So what are you trying to achieve in the code that throws the error? yesterday
和month
都是日期时间,因为那是您在第2行和第4行中定义它们的方式。那么您要在引发错误的代码中尝试实现什么? As the message says, you can't pass a datetime as the day or month parameter to construct another datetime. 如消息所示,您不能将日期时间作为天或月参数来构造另一个日期时间。 Especially as, surely,
yesterday
is already the date that you want? 尤其是,当然,
yesterday
已经是您想要的日期? Why can't you simply pass that to the query? 为什么不能简单地将其传递给查询?
Try this, 尝试这个,
day_ago = datetime.date.today() - datetime.timedelta(days=1)
yesterday = datetime.datetime(day_ago.year, day_ago.month, day_ago.day)
hist_obj = Events.objects.filter(uploader = request.user,
start_date__lt = yesterday)
return render_to_response('history.html', {'history_obj':hist_obj})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.