You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was using this class to validate datetime objects in various queries. However, due to various circumstances, the datetime objects inside these queries were stored as strings. I created this method below to solve this issue:
from datetime import datetime
from django.utils.dateparse import parse_datetime
def convert_to_datetime(data: Any) -> Any:
"""Recursive Helper function that converts datetime objects to
proper type
Returns:
Object: data dictionary that has had its datetime values
converted
"""
if isinstance(data, dict):
for k, v in data.items():
data[k] = convert_to_datetime(v)
elif isinstance(data, list):
for i,e in enumerate(data):
data[i] = convert_to_datetime(e)
elif isinstance(data, str):
try:
dateObj = parse_datetime(data)
if dateObj is None:
return data
else:
return dateObj
except ValueError:
pass
return data
I was using this class to validate datetime objects in various queries. However, due to various circumstances, the datetime objects inside these queries were stored as strings. I created this method below to solve this issue:
I would be happy to contribute this code to the repository if a fix for this issue does not exist.
The text was updated successfully, but these errors were encountered: