MongoKit 无模式
我有一个如下所示的对象:
@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误。 v0.7.1 中已修复
It was a bug. Fixed in v0.7.1