Mongoengine ReferenceField问题

时间:2012-02-07 19:56:18

标签: python mongodb mongoengine

我想创建一个引用字段,该引用字段采用与它所属的文档类型相同的文档类型,但它不起作用,我不知道如何解决这个问题。我忘记了什么或者我必须以另一种方式做到这一点吗?

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。

1 个答案:

答案 0 :(得分:1)

目前,参考字段仅支持文档引用而非嵌入文档。