这是我得到的:
from sqlalchemy import *
from sqlalchemy.orm import *
from web.models.card import *
connectionString = "postgresql://www:www@localhost/prod"
databaseEngine = create_engine(connectionString)
sessionFactory = sessionmaker(autoflush = True, autocommit = False, bind = databaseEngine)
session = sessionFactory()
CardsCollection = session.query(card).all()
_content = {}
for index in range(0, len(CardsCollection)):
c = CardsCollection[index]
_content[index] = c
print json.dumps(_content)
这是错误:
Traceback (most recent call last):
File "/home/src/py/raspberry/src/dictionaryTest.py", line 15, in
CardsCollection = session.query(card).all()
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/query.py", line 1453, in all
return list(self)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/query.py", line 1676, in instances
rows = [process[0](row, None) for row in fetch]
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/mapper.py", line 2234, in _instance
populate_state(state, dict_, row, isnew, only_load_props)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/mapper.py", line 2113, in populate_state
populator(state, dict_, row)
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/strategies.py", line 127, in new_execute
dict_[key] = row[col]
TypeError: 'instancemethod' object does not support item assignment
有人可以帮我这个忙吗?我已经尝试了几件事,并且研究了字典的工作方式……但是它并没有让我失望。
显然,在卡模型上覆盖
self.__dict__(self)
方法是这样做的。我不确定为什么。 最佳答案
__dict__
是保存实例当前状态的special attribute,用方法覆盖它肯定会带来麻烦。
关于python - SQL炼金术> TypeError:'instancemethod'对象不支持项目分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3653690/