Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从具有范围的数据库中获取数据?_Python_Django_Database - Fatal编程技术网

Python 如何从具有范围的数据库中获取数据?

Python 如何从具有范围的数据库中获取数据?,python,django,database,Python,Django,Database,所以我想设定一个从特定日期到当前日期的范围 我这样做: context_data['newNotifications'] = Notification.objects.filter( emailStudent=request.user).filter(time=request.user.viewedNotifications).count() 但是我想要时间值范围从request.user.viewedNotifications到当前时间的所有记录。您看到了吗 基本上你可以做到

所以我想设定一个从特定日期到当前日期的范围

我这样做:

    context_data['newNotifications'] = Notification.objects.filter(
    emailStudent=request.user).filter(time=request.user.viewedNotifications).count()
但是我想要
时间
值范围从
request.user.viewedNotifications
到当前时间的所有记录。

您看到了吗

基本上你可以做到:

导入日期时间
# ...
context_data['newNotifications']=Notification.objects.filter(
emailStudent=request.user).filter(
时间范围=(
request.user.viewedNotifications,
datetime.datetime.now()
)
).count()

因此,您的
.filter()
参数将是
time\uu range=(开始日期,结束日期)
其中
start\u date
来自您的请求,
end\u date
datetime.datetime.now()

您能解释一下当前日期和时间的含义吗?您能进一步分享您的模型吗?viewedNotifications=models.DateTimeField(默认值=datetime.datetime.today())Soo current date and hour指的是当前日期和时间2021-05-02 22:08:53.074874+03:00我试过了,但是我得到了一个错误:
request.user.viewedNotifications
的值是多少?它是一个
datetime.datetime
对象吗?还是一根绳子?哦,对不起。现在我看到了“time\uuuuu range”,即uuu range(它是一个函数/属性:))。谢谢大家:)