2012-10-19 27 views
1

我使用的是瓶中mongokit,每次我嘗試使用收集我創造,我收到No collection foundmongokit沒有收集發現

我在單獨的文件中定義models.py我收藏的錯誤。它看起來像這樣:

from mongokit import Connection, Document 
import os 
import sys 

here = os.path.dirname(os.path.abspath(__file__)) 
path = os.path.abspath(os.path.join(here, 'settings')) 
sys.path.append(path) 
from settings import base as settings 

connection = Connection() 

@connection.register 
class Contact(Document): 
    __database__ = settings.MONGO_DBNAME 
    __collection__ = "Contact" 

    structure = { 
     "name":unicode, 
     "mobile_number":unicode, 
    } 

    required_fields = ["name"] 


@connection.register 
class User(Document): 
    __database__ = settings.MONGO_DBNAME 
    __collection__ = 'User' 

    structure = { 
     "username":unicode, 
     "twitter_access_token":unicode, 
     "twitter_token_secret":unicode, 
     "contacts":[Contact] 
    } 
    required_fields = ["username"] 
    default_values = { 
      "twitter_access_token": "", 
      "twitter_token_secret": "" 
     } 

但後來我想:

>>> from models import User 
>>> u = User() 
>>> u["username"] = "somename" 
>>> u.save() 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 404, in save 
    self.validate(auto_migrate=False) 
    File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 230, in validate 
    (size_limit, size_limit_str) = self._get_size_limit() 
    File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 214, in _get_size_limit 
    server_version = tuple(self.connection.server_info()['version'].split(".")) 
    File "/Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/mongokit/document.py", line 622, in __getattribute__ 
    raise ConnectionError('No collection found') 
mongokit.mongo_exceptions.ConnectionError: No collection found 

我跟着this tutorial,而不是連記法connection.<dbname>.<collection>()作品。是的,確實有這樣一個集合。

我錯過了什麼?

回答

3

引述tutorial您鏈接:

To avoid repeating ourselves though, let’s specify the database and collection name in the Document definition:

@connection.register 
class BlogPost(Document): 
    __collection__ = 'blog_posts' 
    __database__ = 'blog' 
    structure = {...} 

>>> bp = connection.BlogPost() 

在殼實施例中,模型對象通過connection對象構成。在你的情況下,你只是在做user = User()。嘗試通過您用於註冊模型的相同connection實例創建用戶(例如user = connection.User())。

+0

不是。 'connection.User()',以及connection.contacts.User()返回以下錯誤: 'Traceback(最近呼叫最後一個): 文件「」,第1行,在 文件「/ Users/fernandocezar/.virtualenvs/contacts/lib/python2.7/site-packages/pymongo/database.py「,第769行,在__call__ self .__ name,self .__ connection .__ class __.__ name__)) TypeError:'Database'object不可調用。如果你打算在'Connection'對象上調用'User'方法,那麼它就會失敗,因爲沒有這樣的方法存在。' –

+0

也許這是因爲模型被註冊到了不同的'Connection'實例,而不是你正在引用的實例貝殼?我剛剛使用本教程創建了一個新腳本,並且如果連接不一樣,就能夠產生錯誤。 – jmikola

+0

是的,這是訣竅!謝謝@jmikola –