在MongoEngine中使用reverse_delete_rule时,如何使用循环或前向ReferenceField?

Min*_*ark 1 forward-declaration cyclic-reference mongoengine

这段代码炸弹:

from mongoengine import *

class Employee(Document):
    name = StringField()
    boss = ReferenceField("Employee", reverse_delete_rule = NULLIFY)
Run Code Online (Sandbox Code Playgroud)

以下是例外情况:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[…]/mongoengine/base.py", line 791, in __new__
    new_class = super_new(cls, name, bases, attrs)
  File "[…]/mongoengine/base.py", line 630, in __new__
    f.document_type.register_delete_rule(new_class,
    File "[…]/mongoengine/fields.py", line 757, in document_type
    self.document_type_obj = get_document(self.document_type_obj)
  File "[…]/mongoengine/base.py", line 136, in get_document
    """.strip() % name)
mongoengine.base.NotRegistered: `Employee` has not been registered
in the document registry.
Importing the document class automatically registers it, has it
been imported?
Run Code Online (Sandbox Code Playgroud)

删除reverse_delete_rule修复问题,但我想有这个规则.

我尝试了这个,但它确实有效,但它真的看起来像废话,我担心可能会有不好的副作用(到目前为止,我还没有见过):

from mongoengine import *

class Employee(Document):
    pass # required for the reverse_delete_rule to work on the boss field,
         # because the Employee class needs to exist.

class Employee(Document):
    name = StringField()
    boss = ReferenceField("Employee", reverse_delete_rule = NULLIFY)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?这不应该被认为是MongoEngine中的一个错误吗?

tbi*_*icr 7

尝试使用'self',而不是'Employee':

from mongoengine import *

class Employee(Document):
    name = StringField()
    boss = ReferenceField("self", reverse_delete_rule = NULLIFY)
Run Code Online (Sandbox Code Playgroud)

详情请见:https://mongoengine-odm.readthedocs.org/en/latest/guide/defining-documents.html#reference-fields.