1๐
A date
has no timezone info, hence that does not work by localizing. What you can do is truncate today and then add this as filtering:
today = datetime.datetime.now()
today = pytz.timezone("Europe/Paris").localize(today, is_dst=None)
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
currentperiod = Period.objects.get(
startdate__lte=today,
enddate__gte=today
)
If you want to check the enddate
by the end of the day, then you can work with:
from datetime import timedelta
today = datetime.datetime.now()
today = pytz.timezone("Europe/Paris").localize(today, is_dst=None)
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
currentperiod = Period.objects.get(
startdate__lte=today,
enddate__gte=today + timedelta(days=1, microseconds=-1)
)
Source:stackexchange.com