I need to compare a date field in my bookings table with today's date. I am trying to loop through all bookings dates and if the date is less than today's date date to change a field status to "Completed"
I keep getting the below error
"TypeError: '<' not supported between instances of 'datetime.date' and 'str'"
I have tried to research this but I am unable to resolve the issue.
Below is my code.
Model.(ASPBookings)
booking_date = models.DateField()
View
date_today = (datetime.datetime.now())
date_stripped = date_today.strftime('%Y-%m-%d')
asp_date_data = ASPBookings.objects.all()
for booking in asp_date_data:
print (booking.booking_date)
if booking.booking_date < date_stripped:
booking.status == 'Completed'
booking.save()
I have checked the output of the fields and after stripping the time the dates appear the same. eg.
ASPBookings field dates 2021-05-27 2021-06-24 2021-06-15
Today's date 2021-06-09
Thanks in advance for any help I can get on this.
The error message is clear, it means you can't compare a date
object (booking.booking_date
's type) with a string (date_stripped
's type). Try this:
date_today = datetime.datetime.now().date() # use this instead of strftime
asp_date_data = ASPBookings.objects.all()
for booking in asp_date_data:
print (booking.booking_date)
if booking.booking_date < date_stripped:
booking.status == 'Completed'
booking.save()