Python 烧瓶MongoKit';收集';对象不可调用

Python 烧瓶MongoKit';收集';对象不可调用,python,python-2.7,flask,mongokit,Python,Python 2.7,Flask,Mongokit,我以此为例 我只是想得到一个用于编写单元测试的文档实例,但它不起作用。以下是测试代码: import unittest from tests import app, db, ctx from word.models import Word class ModelWordTestCase(unittest.TestCase): def setUp(self): pass def test_model_word(self): print db.W

我以此为例

我只是想得到一个用于编写单元测试的文档实例,但它不起作用。以下是测试代码:

import unittest
from tests import app, db, ctx
from word.models import Word

class ModelWordTestCase(unittest.TestCase):

    def setUp(self):
        pass

    def test_model_word(self):
        print db.Word

        word = db.Word()

        self.assertIsNotNone(word)

    def tearDown(self):
        pass
Word类

from flask.ext.mongokit import Document
from core import db

@db.register
class Word(Document):
    __collection__ = 'words'
    use_dot_notation = True

    STATUS = {
        "approved" : 1,
        "pending" : 0,
        "rejected" : -1,
    }

    structure = {
        'lang': unicode,
        'local': unicode,
        'pronunciation' : unicode,
        'meaning': unicode,
        }
令人惊讶的是,当使用
print db.Word
语句打印时,db.Word是存在的,但不能像我在上面提到的教程中那样调用它来创建新实例。以下是测试的输出:

Collection(Database(MongoClient('localhost', 27017), u'words_test'), u'Word')

E
======================================================================
ERROR: test_model_word (tests.model_tests.ModelWordTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests/model_tests.py", line 14, in test_model_word
    word = db.Word()
  File "/usr/local/lib/python2.7/dist-packages/mongokit/collection.py", line 64, in __call__
    self.__name)
TypeError: 'Collection' object is not callable. If you meant to call the 'Word' method on a 'Database' object it is failing because no such method exists.

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

如何修复此问题并获取Word文档实例,以便创建和保存记录。

我认为您在测试中使用了错误的db实例。您应该使用用于注册模型的同一个db实例。因此,我不知道您的代码是如何构造的,但乍一看,您应该从模型中导入db:

>>> from word.models import db
>>> db.Word.find()
或许

>>> from core import db

请编辑您的问题,并在您定义的
Word
中包含代码。使用代码编辑问题。立即检查我停止使用Mongokit,开始直接使用pymongo。它更加灵活,并且正在积极开发。但既然你名声不好,而且给出了合理的答案,我无论如何都会接受你的答案。