How to Integrate Pyramid 1.1 and Mongo DB - as few lines as possible

谁说我不能喝 提交于 2019-12-04 16:24:01

Pymongo's Database and Collection objects respond to __getattr__ in order to provide a nicer interface and let you write code like:

db.foo.bar.find(...)

Any call to __getattr__ will succeed, but unfortunately this confuses some libraries which expect certain attributes to be callable (the Pymongo Collection object is not callable, except to raise that exception you're seeing above).

What I've done in Pyramid projects is only use the database from within the resources, to prevent references to the database or collections from becoming present in the module level in views or other code. As an added benefit, this ends up being a good way of enforcing separation of concerns so that resources handle database manipulation, and views only translate that for display in the templates.

Another possible solution is to use the 'debugtoolbar.panels' setting in your config file to disable the request_vars panel (which is what is causing the issue):

[app:main]
.. other stuff ...
debugtoolbar.panels =
    pyramid_debugtoolbar.panels.versions.VersionDebugPanel
    pyramid_debugtoolbar.panels.settings.SettingsDebugPanel
    pyramid_debugtoolbar.panels.headers.HeaderDebugPanel
#    pyramid_debugtoolbar.panels.request_vars.RequestVarsDebugPanel
    pyramid_debugtoolbar.panels.renderings.RenderingsDebugPanel
    pyramid_debugtoolbar.panels.logger.LoggingPanel
    pyramid_debugtoolbar.panels.performance.PerformanceDebugPanel
    pyramid_debugtoolbar.panels.routes.RoutesDebugPanel
    pyramid_debugtoolbar.panels.sqla.SQLADebugPanel

That error means that your trying to call a method (html) that doesn't exist in the Database instance.

>>> conn = Connection()
>>> db = conn.mydb
>>> col = db.mycoll
>>> col = db.mycoll()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/virtualenvs/myenv/lib/python2.7/site-packages/pymongo-2.0-py2.7-macosx-10.6-x86_64.egg/pymongo/collection.py", line 1156, in __call__
self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'mycoll' method on a 'Database' object it is failing because no such method exists.

If you haven't modified the code then it is possible it's an bug in markupsafe that tries to call html() in a Database instance

s.__html__()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!