Hul*_*ulk 3 python django django-models django-views
在我的观点中,我有以下格式的日期s_date=20090106和e_date=20100106
该模型定义为
class Activity(models.Model):
timestamp = models.DateTimeField(auto_now_add=True)
Run Code Online (Sandbox Code Playgroud)
如何查询使用上述信息提交的时间戳.
Activity.objects.filter(timestamp>=s_date and timestamp<=e_date)
Run Code Online (Sandbox Code Playgroud)
谢谢.....
您必须将日期转换为datetime.datetime类的实例.为您的案例最简单的方法是:
import datetime
#
# This creates new instace of `datetime.datetime` from a string according to
# the pattern given as the second argument.
#
start = datetime.datetime.strptime(s_date, '%Y%m%d')
end = datetime.datetime.strptime(e_date, '%Y%m%d')
# And now the query you want. Mind that you cannot use 'and' keyword
# inside .filter() function. Fortunately .filter() automatically ANDs
# all criteria you provide.
Activity.objects.filter(timestamp__gte=start, timestamp__lte=end)
Run Code Online (Sandbox Code Playgroud)
请享用!