[Answered ]-Error AttributeError: 'str' object has no attribute 'year' when querying a model

1👍

startdate and enddate are strings, not datetime objects. You need to parse these, so something like:

from datetime import datetime

startdate = datetime.strptime(request.POST['startdate'], '%Y-%m-%d')
enddate = datetime.strptime(request.POST['enddate'], '%Y-%m-%d')
queryset = paidparking.objects.all()
qsstats = QuerySetStats(queryset, date_field='expirationdate')
values = qsstats.time_series(startdate, enddate, interval='days')
return render(request, 'index/template.html', {'values': values})

The format here ('%Y-%m-%d') might be different, depending on how the date is formatted as string.

I would however advise to work with a Form [Djang-doc]. These forms make it convenient to parse, clean and validate input.

Leave a comment