如何从数据库中减去当前日期和日期

How to subtract current date with date from database

我有一个捐助者 table 有 lastAttendance 列。如何用选定的上次出席日期减去当天?

d0 = Donor.objects.only("lastAttendance")
d1 = datetime.now()
delta = d1 - d0

错误:

unsupported operand type(s) for -: 'datetime.datetime' and 'QuerySet'

试试下面的代码

from django.utils import timezone  
d0 = Donor.objects.only("lastAttendance")[0]
d1 = timezone.now()
delta = d1 - d0.lastAttendance

Django 有一个 timesince 函数。在文档中阅读它。 https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#timesince