MongoKit 无模式

发布于 2024-11-30 16:50:35 字数 1881 浏览 2 评论 0原文

我有一个如下所示的对象:

@connection.register
class User(Document):
    __collection__ = 'users'
    __database__ = 'crucible_projects'
    use_schemaless = True
    structure = {
        'name': unicode,
        'password': unicode,
        'last_name': unicode,
        'first_name': unicode,
        'email': unicode,
        'last_login': datetime.datetime,
    }
    use_dot_notation = True
    def __repr__(self):
        return '<User %r>' % (self.name)

我已经在数据库中手动输入了一个没有first_name和last_name字段的用户。问题是当我尝试运行这个时

def login_user(user):
    found_attribute = connection.User.find_one({'name':user})
    found_attribute.last_login = datetime.datetime.utcnow()
    found_attribute.save()

,我得到这个

>>> import db_users
>>> db_users.login_user('admin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "db_users.py", line 91, in login_user
  found_attribute.save()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 394, in save
  self.validate(auto_migrate=False)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 243, in validate
  super(Document, self).validate()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 353, in validate
  self._validate_doc(self, self.structure)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 569, in _validate_doc
  "missed fields : %s" % struct_doc_diff )
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 524, in _raise_exception

raise exception(message)
mongokit.schema_document.StructureError: missed fields : ['first_name', 'last_name']

我想要一些不具有结构中所有字段的对象。我做错了什么?

添加了一个答案。

I have an object like follows:

@connection.register
class User(Document):
    __collection__ = 'users'
    __database__ = 'crucible_projects'
    use_schemaless = True
    structure = {
        'name': unicode,
        'password': unicode,
        'last_name': unicode,
        'first_name': unicode,
        'email': unicode,
        'last_login': datetime.datetime,
    }
    use_dot_notation = True
    def __repr__(self):
        return '<User %r>' % (self.name)

I have a user already in the database entered by hand that does not have the first_name and last_name field. Problem is when i try to run this

def login_user(user):
    found_attribute = connection.User.find_one({'name':user})
    found_attribute.last_login = datetime.datetime.utcnow()
    found_attribute.save()

I get this

>>> import db_users
>>> db_users.login_user('admin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "db_users.py", line 91, in login_user
  found_attribute.save()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 394, in save
  self.validate(auto_migrate=False)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\document.py", line 243, in validate
  super(Document, self).validate()
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 353, in validate
  self._validate_doc(self, self.structure)
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 569, in _validate_doc
  "missed fields : %s" % struct_doc_diff )
File "c:\Python27\lib\site-packages\mongokit-0.7-py2.7.egg\mongokit\schema_document.py", line 524, in _raise_exception

raise exception(message)
mongokit.schema_document.StructureError: missed fields : ['first_name', 'last_name']

I would like to have some objects that dont have all the fields in the structure. What am I doing wrong?

Added an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-12-07 16:50:35

这是一个错误。 v0.7.1 中已修复

Changelog :

* change MongokitMasterSlaveConnection to MasterSlaveConnection for consistency
* fix #57 -- support pymongo > 1.9 in grid.py
* fix #45 -- remove automatique index creation
* fix #43 -- slicing a cursor should return a mongokit document, not dict
* Dont try to convert None struct to json (patch from @mLewisLogic thanks !)
* fix schemaless issue (thanks to Mihai Pocorschi for reporting it)

It was a bug. Fixed in v0.7.1

Changelog :

* change MongokitMasterSlaveConnection to MasterSlaveConnection for consistency
* fix #57 -- support pymongo > 1.9 in grid.py
* fix #45 -- remove automatique index creation
* fix #43 -- slicing a cursor should return a mongokit document, not dict
* Dont try to convert None struct to json (patch from @mLewisLogic thanks !)
* fix schemaless issue (thanks to Mihai Pocorschi for reporting it)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文