I use django simple-history to get history on my models I then search the history results by date but I get the error below. How can I format the date?
RuntimeWarning: DateTimeField HistoricalIssue.history_date received a naive datetime (2022-04-13 10:34:32) while time zone support is active.
warnings.warn("DateTimeField %s received a naive datetime (%s)"
def SearchByDate(request):
date_search = request.POST['date-search']
if date_search:
admin_hist_search_results = Issue.history.filter(history_date=date_search)
CodePudding user response:
First, keep in mind that this is not an error, but "only" a warning. It mentions that the incoming timestamp (which you store in variable date_search
) does not have timezone information, while you compare it with a timestamp field (history_date
on model Issue
) that does have timezone information. This could potentially lead to issues.
If you know the timezone coming in from the request, you can add that information to the timestamp, for example:
import pytz
date_as_string = request.POST['date-search']
parsed_date = datetime.strptime(date_as_string, '%Y-%m-%d')
amsterdam_timezone = pytz.timezone('Europe/Amsterdam')
date_search = amsterdam_timezone.localize(parsed_date)