title: TypeError: Object of type 'datetime' is not JSON serializable (with serialize function)
body: <p>I'm  I getting this <code>TypeError: Object of type 'datetime' is not JSON serializable</code> error, even though I have a specific serialize function described in my model.</p>
  
  <p>This is my code:</p>
  
  <p>Flask route (rendered by React):</p>
  
  <p><strong>menus.py</strong></p>
  
  <pre><code>@menus_bp.route('/menus', methods=['GET', 'POST'])
  def menus():
  
      response_object = {
          'status': 'fail',
          'message': 'Invalid payload.'
          }
      try:
          user = User.query.filter_by(id=1).first()
          if user.menu == []:
              return edit_menu()
          else:
              template = render_template('menus.html')
              response_object = {
                  'status': 'success',
                  'message': 'success',
                  'data': [{"restaurant": user.restaurant,
                            "menu": menu,
                            "content": template}] # template passed to React
                  }
  
              # db method
              Create_Menu(user=user)
  
          return jsonify(response_object), 200
      except (exc.IntegrityError, ValueError):
          db.session.rollback()
          return jsonify(response_object), 400
  </code></pre>
  
  <p><strong>methods.py</strong></p>
  
  <pre><code>def Create_Menu(user):
      menu = Menu(user=user)
      db.session.add(menu)
      db.session.commit()
  
      return {"status": True,
              "menu": menu}
  </code></pre>
  
  <p>and finally the Menu model, which has a <code>serialize()</code> function:</p>
  
  <pre><code>class Menu(db.Model):
      __tablename__='menu'
      """
      Model for storing menus. 
      """   
      id = db.Column(db.Integer, primary_key=True)
      created = db.Column(db.DateTime, default=func.now(), nullable=False)       
      user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  
      def __init__(self, user):
          self.user = user
  
      def serialize(self):
         return { 'id' : self.id,
                  'created': self.created,
                  'coffees' : [ item.serialize() for item in self.coffees]}
  </code></pre>
  
  <p>But I'm getting the following traceback:</p>
  
  <pre><code>TypeError: Object of type 'datetime' is not JSON serializable
  
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
  return self.wsgi_app(environ, start_response)
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
  response = self.handle_exception(e)
  File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
  return original_handler(e)
  File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
  reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
  raise value.with_traceback(tb)
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
  response = self.full_dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
  rv = self.handle_user_exception(e)
  File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 269, in error_router
  return original_handler(e)
  File "/usr/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function
  return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
  reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3.6/site-packages/flask/_compat.py", line 34, in reraise
  raise value.with_traceback(tb)
  File "/usr/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
  rv = self.dispatch_request()
  File "/usr/lib/python3.6/site-packages/flask_debugtoolbar/__init__.py", line 125, in dispatch_request
  return view_func(**req.view_args)
  File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 462, in wrapper
  return self.make_response(data, code, headers=headers)
  File "/usr/lib/python3.6/site-packages/flask_restful/__init__.py", line 491, in make_response
  resp = self.representations[mediatype](data, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/flask_restful/representations/json.py", line 21, in output_json
  dumped = dumps(data, **settings) + "\n"
  File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
  **kw).encode(obj)
  File "/usr/lib/python3.6/json/encoder.py", line 201, in encode
  chunks = list(chunks)
  File "/usr/lib/python3.6/json/encoder.py", line 430, in _iterencode
  yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 325, in _iterencode_list
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
  yield from chunks
  File "/usr/lib/python3.6/json/encoder.py", line 437, in _iterencode
  o = _default(o)
  File "/usr/lib/python3.6/json/encoder.py", line 180, in default
  o.__class__.__name__)
  TypeError: Object of type 'datetime' is not JSON serializable
  </code></pre>
  
  <p>It used to work when I was redering templates with Flask at backend, but now with frontend requets, it breaks with the erro above.</p>
  
  <p>what is wrong now? why does not my serialize function work anymore? </p>