我有以下代码:
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__
没有运行,我能为此做些什么?
答案 0 :(得分:27)
查看SQLAlchemy documentation on reconstruction:
在重新创建对象时,SQLAlchemy ORM不会调用
__init__
数据库行。 ORM的过程有点类似于Python 标准库的pickle模块,调用低级__new__
方法,然后直接在实例上静默恢复属性 而不是致电__init__
。如果您之前需要在数据库加载的实例上进行一些设置 他们准备好使用,你可以使用@reconstructor装饰器来标记 作为
__init__
的ORM对应方法。 SQLAlchemy会调用它 每次加载或重建其中一个时都没有参数的方法 你的实例。这对于重建瞬态属性很有用 通常在__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 = []
执行obj = MyMappedClass()时,Python调用
__init__
方法正常,数据参数是必需的。当实例是 在查询操作期间加载,如查询(MyMappedClass).one(),init_on_load
被称为。{/ p>