使用Flask和MongoEngine开发Tumblelog应用程序时出错

时间:2015-04-03 15:12:17

标签: python mongodb python-2.7 flask

我正在尝试按照下面的教程,但是当我运行manage.py时,我遇到的问题很少。任何帮助都可以提供很大帮助。

http://docs.mongodb.org/ecosystem/tutorial/write-a-tumblelog-application-with-flask-mongoengine/#id1

manage.py运行输出:

 (Tumbler)afiz Tumbler $ python manage.py 
Traceback (most recent call last):
  File "manage.py", line 6, in <module>
    from tumblelog import app
  File "/home/afiz/.virtualenvs/tumblelog/__init__.py", line 18, in <module>
    register_blueprints(app)
  File "/home/afiz/.virtualenvs/tumblelog/__init__.py", line 13, in register_blueprints
    from tumblelog.views import posts
  File "/home/afiz/.virtualenvs/tumblelog/views.py", line 5, in <module>
    from tumblelog.models import Post, Comment
  File "/home/afiz/.virtualenvs/tumblelog/models.py", line 6, in <module>
    class Post(db.DynamicDocument):
  File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/metaclasses.py", line 361, in __new__
    meta['index_specs'] = new_class._build_index_specs(meta['indexes'])
  File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 722, in _build_index_specs
    unique_indices = cls._unique_with_indexes()
  File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/document.py", line 861, in _unique_with_indexes
    field.document_type != cls):
  File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/fields.py", line 563, in document_type
    self.document_type_obj = get_document(self.document_type_obj)
  File "/home/afiz/.virtualenvs/Tumbler/local/lib/python2.7/site-packages/mongoengine/base/common.py", line 25, in get_document
    """.strip() % name)
mongoengine.errors.NotRegistered: `Comment` has not been registered in the document registry.
            Importing the document class automatically registers it, has it
            been imported?

manage.py文件:

    #set the path
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from flask.ext.script import Manager, Server
from tumblelog import app

manager = Manager(app)

# Turn on debugger by default and reloader
manager.add_command("runserver", Server(
    use_debugger = True,
    use_reloader = True,
    host = '0.0.0.0')
)

if __name__ == "__main__":
    manager.run()

2 个答案:

答案 0 :(得分:18)

我遇到了与你现在面临的问题相同的问题。在models.py文件中,我刚写了

  

class Comment(db.EmbeddedDocument):

然后首先添加内容

  

class Post(db.Document):

然后是它的内容。换句话说,我首先写了评论课,然后写了课,问题得到了解决;):)干杯!!

答案 1 :(得分:1)

Inside Post,当EmbeddedDocumentField被分配给变量时,需要预先注册。因此,在使用它们之前,始终注册这样的字段,即注释。