Home > database >  Django 'time__contains' is not working in particular case
Django 'time__contains' is not working in particular case

Time:10-28

I'm trying to compare time pasted to form with time saved in DB for deleting this record. I don't understand why it's not working when in DB is stored 8:15:00 and the user pasted 08:15. The field in DB is TimeField and for comparing I'm using .filter(time__contains=t) where t is string pasted from form. Only difference is in zero before 8. When in DB is it stored like 08:15:00, all is working.

class WateringSchedule(models.Model):
    time = models.TimeField()

t = '08:15'

print(WateringSchedule.objects.filter(time__contains=t).count())

Thanks.

CodePudding user response:

You should try icontains instead of contains.

CodePudding user response:

I've only seen contains used with CharField. Here you should use time instead:

print(WateringSchedule.objects.filter(time__time=t).count())

Here I assume Django will parse the input string into a datetime.time object. You may need to use strptime() to do so manually.

Alternatively, you can use hour and minute.

  • Related