TypeError: Объект типа 'datetime' не сериализуем в формате JSON (с функцией serialize) - PullRequest
0 голосов
/ 12 июня 2019

Я получаю эту ошибку TypeError: Object of type 'datetime' is not JSON serializable, хотя в моей модели описана определенная функция сериализации.

Это мой код:

Маршрут колбы (отображается React):

menus.py

@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

Methods.py

def Create_Menu(user):
    menu = Menu(user=user)
    db.session.add(menu)
    db.session.commit()

    return {"status": True,
            "menu": menu}

и, наконец, модель меню,которая имеет serialize() функцию:

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]}

Но я получаю следующий результат:

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

Раньше он работал, когда я рендерил шаблоны с Flask на бэкэнде, нотеперь с помощью внешнего интерфейса, он выходит из строя с ошибкой выше.

что не так сейчас?почему моя функция сериализации больше не работает?

1 Ответ

0 голосов
/ 12 июня 2019

См. Ниже (с использованием пользовательского кодера JSON)

См. Также http://flask.pocoo.org/snippets/119/

import datetime
import json


class DateTimeEncoder(json.JSONEncoder):
    def default(self, z):
        if isinstance(z, datetime.datetime):
            return (str(z))
        else:
            return super().default(z)


my_dict = {'date': datetime.datetime.now()}

print(json.dumps(my_dict,cls=DateTimeEncoder))

выход

{"date": "2019-06-12 15:44:14.978766"}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...