我有以下相互依赖的模型:
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?
如果我删除第一个reverse_delete_rule
:
from mongoengine import *
class DocumentA(Document):
docB = ReferenceField('DocumentB')
class DocumentB(Document):
docA = ReferenceField('DocumentA', reverse_delete_rule=CASCADE)
工作正常。
有没有办法保留reverse_delete_rule
?
答案 0 :(得分:0)
来自MongoEngine
ReferenceField documentation:
注册删除规则的替代语法(在使用时有用) 实施双向删除规则)
class Bar(Document): content = StringField() foo = ReferenceField('Foo') Foo.register_delete_rule(Bar, 'foo', NULLIFY)
所以你的代码变成了:
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)