'Post' 对象没有属性 '_sa_instance_state'

'Post' object has no attribute '_sa_instance_state'

这是站点 https://asperitas.now.sh

的 API

我正在尝试从我的数据库中获取所有帖子,如下所示:

(link 对于 gh: https://github.com/Jonny-programmer/tgym_api)

    def get_all(self):
        print("Here the problem starts")
        return tuple(self.db_sess.query(Post).all())

我得到以下回溯:

Here the problem starts
---> _sa_instance_state
127.0.0.1 - - [25/May/2022 19:17:06] "GET /api/posts/ HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 324, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python3.9/site-packages/werkzeug/serving.py", line 313, in execute
    application_iter = app(environ, start_response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 467, in wrapper
    resp = resource(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask/views.py", line 84, in view
    return current_app.ensure_sync(self.dispatch_request)(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/flask_restful/__init__.py", line 582, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/Users/eremin/Yandex.Disk.localized/eremin_i/Tgym_api/app/api/posts_api.py", line 33, in get
    return jsonify(main_app.posts_repo.get_all())
  File "/Users/eremin/Yandex.Disk.localized/eremin_i/Tgym_api/app/resources/posts_repo.py", line 13, in get_all
    return tuple(self.db_sess.query(Post).all())
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/query.py", line 2759, in all
    return self._iter().all()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 1362, in all
    return self._allrows()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 401, in _allrows
    rows = self._fetchall_impl()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 1275, in _fetchall_impl
    return self._real_result._fetchall_impl()
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/result.py", line 1687, in _fetchall_impl
    return list(self.iterator)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 151, in chunks
    rows = [proc(row) for row in fetch]
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 151, in <listcomp>
    rows = [proc(row) for row in fetch]
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/loading.py", line 934, in _instance
    state = instance_state(instance)
  File "/Users/eremin/Yandex.Disk.localized/eremin_i/Tgym_api/app/data/posts.py", line 29, in __getattr__
    return self.__getattribute__(item)
AttributeError: 'Post' object has no attribute '_sa_instance_state'

我的 SqlAlchemy class 结构:

class Post(SqlAlchemyBase, dict):
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__
    __tablename__ = 'posts'

    id = sqlalchemy.Column(sqlalchemy.Integer,
                           primary_key=True, autoincrement=True)
    user_id = sqlalchemy.Column(sqlalchemy.Integer,
                                sqlalchemy.ForeignKey("users.id"))
    title = sqlalchemy.Column(sqlalchemy.String, nullable=False)
    category = sqlalchemy.Column(sqlalchemy.String, nullable=False, index=True)
    type = sqlalchemy.Column(sqlalchemy.String, nullable=False)
    created = sqlalchemy.Column(sqlalchemy.DateTime, default=datetime.now())

    text = sqlalchemy.Column(sqlalchemy.String, nullable=True)
    url = sqlalchemy.Column(sqlalchemy.String, nullable=True)
    # Пользователь, который создал этот пост
    author = orm.relation('User')

    def __getattr__(self, item):
        print("--->", item)
        return self.__getattribute__(item)

If I'd not inherit my sqlalchemy class from base dict one, doesn't work the another part of application

from fastapi.encoders import jsonable_encoder

obj = jsonable_encoder(your object)

del obj['_sa_instance_state']

因为 SQLAlchemy 对象总是有这个属性