Sql炼金术>类型错误:“实例方法”对象不支持项目分配

发布于 2024-09-18 10:43:50 字数 1663 浏览 8 评论 0原文

这是我所得到的:


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) 方法就是这样做的。但我并不完全确定为什么。

Here's what I've got:


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)

And here's the error:


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

Can someone help me out with this? I've tried a few things, and researched into how dictionaries work... but its just not jumping out at me.

[edit for strange resolution] Apparently, overriding the self.__dict__(self) method on the card model is what did it. I'm not entirely sure why, though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三岁铭 2024-09-25 10:43:50

__dict__ 是一个特殊属性,保存当前实例状态下,用with方法覆盖它肯定会带来麻烦。

__dict__ is a special attribute holding current state of instance, overwriting it with with method will certainly lead to troubles.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文