If you have JSON objects with datetime attributes that you want to decode to python datetime objects, you can use decode_datetime as a simplejson object hook. simplejson.loads(s, object_hook=decode_datetime).
1 2 3 4 5 6 7 8 9 | import dateutil.parser
def decode_datetime(obj):
if 'datetime' not in obj:
return obj
dt = dateutil.parser.parse(obj['datetime'])
obj['datetime'] = dt
return obj
|
More like this
- find even number by Rajeev529 2 weeks, 3 days ago
- Form field with fixed value by roam 1 month, 1 week ago
- New Snippet! by Antoliny0919 1 month, 2 weeks ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 4 months ago
- get_object_or_none by azwdevops 7 months, 3 weeks ago
Comments
If you have datetime objects as "%Y-%m-%d %H:%M:%S", then you don't need to convert them. Django will allow for using these in queries, and saving to the database.
Same with date ("%Y-%m-%d") and time ("%H:%M:%S") objects.
#
Please login first before commenting.