python - SQLAlchemy __init__ 未运行

标签 python sqlalchemy

我有以下代码:

session = scoped_session(sessionmaker(autocommit=False, autoflush=True, bind=engine))

Base = declarative_base()
Base.query = session.query_property()

class CommonBase(object):
  created_at = Column(DateTime, default=datetime.datetime.now)
  updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now)

class Look(Base, CommonBase):
  __tablename__ = "looks"
  id = Column(Integer, primary_key=True)

  def __init__(self):
    print "__init__ is run"
    Base.__init__(self)
    self.feedback = None

  def set_feedback(self, feedback):
    """Status can either be 1 for liked, 0 no response, or -1 disliked.
    """
    assert feedback in [1, 0, -1]
    self.feedback = feedback

  def get_feedback(self):
    return self.feedback

我收到以下错误:

Traceback (most recent call last):
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/web/backend.py", line 94, in wrapped
    ret = f(*args, **kwargs)
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/web/backend.py", line 81, in decorated
    return f(*args, **kwargs)
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/web/backend.py", line 187, in next
    json_ret = ge.encode(results)     # automatically pulls the tags
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/__init__.py", line 54, in default
    jsonable = self.convert_to_jsonable(obj)
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/__init__.py", line 40, in convert_to_jsonable
    image_url=obj.image_url, feedback=obj.get_feedback())
  File "/Volumes/Data2/Dropbox/projects/Giordano/src/giordano/models.py", line 100, in get_feedback
    return self.feedback
AttributeError: 'Look' object has no attribute 'feedback'

在我看来,我的 __init__ 方法没有运行,因为我在日志中看不到任何打印语句。

有人可以解释为什么我的 __init__ 没有运行吗?我该怎么做?

最佳答案

查看 SQLAlchemy documentation on reconstruction :

The SQLAlchemy ORM does not call __init__ when recreating objects from database rows. The ORM’s process is somewhat akin to the Python standard library’s pickle module, invoking the low level __new__ method and then quietly restoring attributes directly on the instance rather than calling __init__.

If you need to do some setup on database-loaded instances before they’re ready to use, you can use the @reconstructor decorator to tag a method as the ORM counterpart to __init__. SQLAlchemy will call this method with no arguments every time it loads or reconstructs one of your instances. This is useful for recreating transient properties that are normally assigned in your __init__:

from sqlalchemy import orm

class MyMappedClass(object):
    def __init__(self, data):
        self.data = data
        # we need stuff on all instances, but not in the database.
        self.stuff = []

    @orm.reconstructor
    def init_on_load(self):
        self.stuff = []

When obj = MyMappedClass() is executed, Python calls the __init__ method as normal and the data argument is required. When instances are loaded during a Query operation as in query(MyMappedClass).one(), init_on_load is called.

关于python - SQLAlchemy __init__ 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16156650/

相关文章:

Python 缓存 : TypeError: unhashable type: 'dict'

python - 如何在 Django 管理中以表格格式显示添加模型?

Python SQLAlchemy 如何通过排除选定列进行查询

python - 从经过训练的自动编码器中提取编码器和解码器

Python - NumPy - 从数组中删除多行和多列

python - numpy 对每一行进行排序并检索第 k 个元素

mysql - SQLAlchemy/MySQL 二进制 blob 正在使用 utf-8 编码吗?

python - pandas dataframe to sql - 将数据附加到现有表而不重复

python - 在集成测试 (Pytest) 和 Flask 应用程序之间创建不同的 SQLAlchemy session

python - 使用新值更新 Enum 后,无法使用 psycopg2 插入该值