0

I have a Donor table with lastAttendance column. How can I subtract current day with selected lastAttendance date?

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

Error:

unsupported operand type(s) for -: 'datetime.datetime' and 'QuerySet'
slavoo
  • 5,798
  • 64
  • 37
  • 39
kasia
  • 288
  • 2
  • 6
  • 23

2 Answers2

0

Try the following code

from django.utils import timezone  
d0 = Donor.objects.only("lastAttendance")[0]
d1 = timezone.now()
delta = d1 - d0.lastAttendance
Arun
  • 1,933
  • 2
  • 28
  • 46
  • still the same error: unsupported operand type(s) for -: 'datetime.datetime' and 'QuerySet' – kasia Jun 19 '17 at 11:07
  • @kasia `Donor.objects.only("lastAttendance")` returns a queryset. In order to get results, you should iterate through the list. Also please note the update. – Arun Jun 19 '17 at 11:24
  • @kasia: If you want to iterate the difference in a template, you can iterate through the objects, then store the difference in a list. In the template, you can use a for loop to display the values. – Arun Jun 19 '17 at 11:44
0

There is a timesince function for Django. Read about it in the docs. https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#timesince

hansTheFranz
  • 2,511
  • 4
  • 19
  • 35