简体   繁体   English

Flask / PyMongo-在应用程序顶部初始化pymongo

[英]Flask/PyMongo - Initialize pymongo at the top of the app

I'm trying to scale up my first Flask app and am not understanding the structure needed to use a pymongo db in multiple modules. 我正在尝试扩展我的第一个Flask应用程序,并且不了解在多个模块中使用pymongo db所需的结构。 For example, here is my new structure: 例如,这是我的新结构:

run.py
app/
├── __init__.py
├── forms.py
├── static/
├── templates/
└── views/
    ├── __init__.py
    ├── bookmarklet.py
    ├── main.py
    └── user.py

Prior to trying to scale this, I had this at the top of my single views.py file: 在尝试进行缩放之前,我将其放在单个views.py文件的顶部:

from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
with app.app_context():
    mongo.db.user.ensure_index("email", unique=True)

The goal is to be able to use this mongo instance in all of the view modules as well as the forms.py module. 目的是能够在所有视图模块以及forms.py模块中使用此mongo实例。 I've tried these two things: 我已经尝试了以下两件事:

  1. Put the above snippet in the app/__init__.py file, but can't seem to make it accessible to any other modules. 将以上代码段放入app/__init__.py文件中,但似乎无法使其他任何模块都可以访问它。 I tried doing this: app.db = mongo.db (but it wasn't available downstream) 我尝试这样做: app.db = mongo.db (但下游不可用)
  2. Put the above snippet into each module that needs it, but then I get the error that there are multiple mongo instances with the same prefix. 将上面的代码片段放入需要它的每个模块中,但是然后我得到一个错误,即有多个具有相同前缀的mongo实例。

Where should this initialization go in order to make it accessible everywhere in the app? 为了使它在应用程序中的任何地方都可以访问,该初始化应该去哪里?

EDIT 编辑

It sounds like I'm doing it right but there is something else going on. 听起来我做对了,但是还有其他事情在发生。 I'm posting my more complete code and error. 我要发布更完整的代码和错误。

app/__init__.py 应用程序/ __ init__.py

from flask import Flask

app = Flask(__name__)
from app.views import main

app.config.update(
    DEBUG = True,
    SECRET_KEY = "not telling",
    WTF_CSRF_ENABLED = False,
)

app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')

from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
with app.app_context():
    mongo.db.user.ensure_index("email", unique=True)

app/views/main.py 应用/视图/ main.py

from app import app
from flask import render_template, redirect, request, flash, url_for
from flask.ext.jsonpify import jsonify
from app.forms import *

from app import *
print mongo

Error: 错误:

(venv)imac: me$ ./run.py 
Traceback (most recent call last):
  File "./run.py", line 4, in <module>
    from app import app
  File "/Users/me/Dropbox/development/test/app/__init__.py", line 4, in <module>
    from app.views import main
  File "/Users/me/Dropbox/development/test/app/views/main.py", line 9, in <module>
    print mongo
NameError: name 'mongo' is not defined

Put that snippet in app/__init__.py . 将该代码段放入app/__init__.py If you want to access it in forms.py , for instance, try: 例如,如果要在forms.py访问它,请尝试:

...
from app import *
# then you can use mongo here
print mongo
...

If you want to access it in user.py , for instance, try the same code above. 例如,如果要在user.py访问它,请尝试上面的相同代码。

Check if this works for you. 检查是否适合您。 If not, show me the error message and I will think about solution two. 如果不是,请显示错误消息,然后考虑解决方案二。

SOLVED 解决了

The mistake in my __init__.py file was that I was importing my views to early. 我的__init__.py文件中的错误是我要尽早导入视图。 You have to do that at the end! 您必须在最后做!

WRONG 错误

from flask import Flask

app = Flask(__name__)
from app.views import main # <- TOO EARLY

app.config.update(
    DEBUG = True,
    SECRET_KEY = "not telling",
    WTF_CSRF_ENABLED = False,
)

app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')

from flask.ext.pymongo import PyMongo
mongo = PyMongo(app)
with app.app_context():
    mongo.db.user.ensure_index("email", unique=True)

RIGHT

from flask import Flask
from flask.ext.pymongo import PyMongo

app = Flask(__name__)

mongo = PyMongo(app)
with app.app_context():
    mongo.db.user.ensure_index("email", unique=True)

app.config.update(
    DEBUG = True,
    SECRET_KEY = "not telling",
    WTF_CSRF_ENABLED = False,
)

app.jinja_env.add_extension('pyjade.ext.jinja.PyJadeExtension')

# AT THE BOTTOM!
from app.views import main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM