我想创建一个参考字段,它采用与它将成为成员的文档类型相同的文档类型,但它不起作用,我不知道如何解决这个问题。我是否忘记了什么,或者我必须以其他方式来做?
import mongoengine as mongo
class AuthRequest(mongo.EmbeddedDocument):
user_id = mongo.IntField(required=True, min_value=0)
message = mongo.StringField(required=True, max_length=256)
class DatabaseUser(mongo.EmbeddedDocument):
id = mongo.IntField(primary_key=True, min_value=0)
name = mongo.StringField(required=True, unique=True, max_length=24)
passw = mongo.StringField(required=True)
mail = mongo.EmailField(required=True)
last_online = mongo.DateTimeField()
contact_field = mongo.ReferenceField('self',
reverse_delete_rule=mongo.NULLIFY)
contacts = mongo.ListField(contact_field)
requests = mongo.ListField(mongo.EmbeddedDocumentField(AuthRequest))
class UserCollection(mongo.Document):
users = mongo.ListField(mongo.EmbeddedDocumentField(DatabaseUser))
meta = {'collection': 'users'}
这是我在使用“DatabaseUser”时遇到的错误:
Traceback (most recent call last):
File "path_to_my_script.py", line 206, in <module>
class DatabaseUser(mongo.EmbeddedDocument):
File "E:\Python27\lib\site-packages\mongoengine\base.py", line 401, in __new__
field.document_type.register_delete_rule(new_class, field.name,
File "E:\Python27\lib\site-packages\mongoengine\fields.py", line 605, in document_type
self.document_type_obj = get_document(self.document_type_obj)
File "E:\Python27\lib\site-packages\mongoengine\base.py", line 42, in get_document
""".strip() % name)
mongoengine.base.NotRegistered: `DatabaseUser` has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
这是使用“self”时的错误:
Traceback (most recent call last):
File "path_to_my_script.py", line 206, in <module>
class DatabaseUser(mongo.EmbeddedDocument):
File "E:\Python27\lib\site-packages\mongoengine\base.py", line 401, in __new__
field.document_type.register_delete_rule(new_class, field.name,
AttributeError: type object 'DatabaseUser' has no attribute 'register_delete_rule'
我在 Windows 7(64 位)上使用 Python 2.7.2(64 位)和 Mongoengine v.5。