简体   繁体   English

“必须为整数”日期时间字段

[英]“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? yesterdaymonth都是日期时间,因为那是您在第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.

相关问题 迭代日期时间-TypeError:必须为整数 - Iterative Datetime - TypeError: an integer is required pymongo datetime TypeError:必须为整数 - pymongo datetime TypeError: an integer is required 将datetime.datetime与datetime.time相结合 - TypeError:需要一个整数 - combining datetime.datetime with datetime.time - TypeError: an integer is required Python - 类型错误:需要一个 integer(获取类型 datetime.datetime) - Python - TypeError: an integer is required (got type datetime.datetime) 类型错误:需要一个整数(获取类型 datetime.datetime) - Type error : an integer is required (got type datetime. datetime) 从日期时间字段中减去整数值 - substract integer value from datetime field datetime.date:TypeError:必须为整数。 为什么? - datetime.date: TypeError: an integer is required. Why? Python datetime.date issue TypeError: an integer is required - Python datetime.date issue TypeError: an integer is required 类型错误:需要一个 integer(获取类型元组)日期时间 Python - TypeError: an integer is required (got type tuple) datetime Python 类型错误:尝试将字典中的日期时间对象转换为字符串时需要整数(获取类型 datetime.datetime) - TypeError: an integer is required (got type datetime.datetime) when trying to convert a datetime object within a dictionary to a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM