Mongoengine ReferenceField 问题
我想创建一个引用字段,这个字段应该和它所属的文档类型相同,但现在不管怎么做都不行,我也不知道该怎么解决。是不是我漏掉了什么,还是说我需要换个方法?
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'
我现在使用的是Python 2.7.2(64位)和Mongoengine v.5,操作系统是Windows 7(64位)。
1 个回答
1
目前,引用字段只支持文档引用,而不支持嵌入的文档。