In my controller, I eventually call json.dump() on a list that contains created_on values (ex: datetime.datetime(2020, 4, 26, 10, 18, 13, tzinfo=<UTC>)), to return it as JSON serialized back to a DataTable (https://datatables.net/) data source axaj call.
Unfortunately, I get the following Exception:
TypeError: datetime.datetime(2020, 4, 26, 10, 18, 13, tzinfo=<UTC>) is not JSON serializable
Your list is based on a model, right? Let’s assume your model is named Invoice. So you define a schema like this:
class InvoiceSchema(mm.ModelSchema):
class Meta:
model = Invoice
# list everything from your model that you want to include in the JSON data
fields = ('id', 'created_on', ...)
Then, in your controller where you have a list of Invoice objects, you do this (if you want the RH itself to return the JSON list)
FWIW, if you use dump instead of dumps you get a Python object that can be jsonified later (e.g. using the |tojson filter in a Jinja template).
Using Marshmallow schemas like this is the way to go for any object that should be converted to JSON, since it ensures you have a well-defined structure for it.
However, if your code is currently as simple as creating some dicts that are eventually jsonified, you can also simply call .isoformat() on the datetime object to get a string.