Django development of Datetime type JSON serialization times error
Prerequisite review
When doing django development view views, if the database field is of type datetime, an exception occurs when JSON serialization is returned
Exceptions
TypeError: Object of type datetime is not JSON serializable
Cause of the exception
'create_time': datetime.datetime(2020, 6, 1, 17, 27, tzinfo=<UTC>)
The field is datetime and the object cannot be serialized
Problem solved
# format the time # create_time database table field name for t in Students.objects.all(): 'create_time': t.create_time.strftime ('%Y-%m-%d %H:%M:%S'),