Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Mongoengine中带反向删除规则的循环依赖_Python_Mongoengine_Circular Dependency - Fatal编程技术网

Python Mongoengine中带反向删除规则的循环依赖

Python Mongoengine中带反向删除规则的循环依赖,python,mongoengine,circular-dependency,Python,Mongoengine,Circular Dependency,我有以下相互依赖的模型: from mongoengine import * class DocumentA(Document): docB = ReferenceField('DocumentB', reverse_delete_rule=CASCADE) class DocumentB(Document): docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE) 我得到以下错误: mongoengi

我有以下相互依赖的模型:

from mongoengine import *

class DocumentA(Document):
    docB = ReferenceField('DocumentB', reverse_delete_rule=CASCADE)

class DocumentB(Document):
    docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE)
我得到以下错误:

mongoengine.errors.NotRegistered: `DocumentB` has not been registered in the document registry.
            Importing the document class automatically registers it, has it
            been imported?
如果删除第一条
反向删除规则
,此错误将消失:

from mongoengine import *

class DocumentA(Document):
    docB = ReferenceField('DocumentB')

class DocumentB(Document):
    docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE)
很好

有没有办法保持
反向删除规则

来自:

用于注册删除规则的替代语法(在 实现双向删除规则)

因此,您的代码变成:

from mongoengine import *

class DocumentA(Document):
    docB = ReferenceField('DocumentB')

class DocumentB(Document):
    docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE)

DocumentB.register_delete_rule(DocumentA, 'docB', CASCADE)
from mongoengine import *

class DocumentA(Document):
    docB = ReferenceField('DocumentB')

class DocumentB(Document):
    docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE)

DocumentB.register_delete_rule(DocumentA, 'docB', CASCADE)