如果我有两个模型,User
和Address
,他们会像这样互相引用:
# usermodel.py
from address import Address
from mongoengine import *
class User(Document):
name = StringField()
address = ReferenceField(Address)
# address.py
from mongoengine import *
class Address(Document):
owner = ReferenceField('User', reverse_delete_rule=2)
我收到错误消息:
NotRegistered('`User` has not been registered in the document registry.
Importing the document class automatically registers it, has it been imported?',)
我可以在一个文档上设置信号,在另一个文档上设置reverse_delete_rule吗?我相信这种情况正在发生,因为reverse_delete_rule需要注册文档。
答案 0 :(得分:0)
这可以通过使用reverse_delete_rule
的备用表示法来解决:
User.register_delete_rule(Address, 'owner', CASCADE)
所以文件会像这样更新:
# usermodel.py
from address import Address
from mongoengine import *
class User(Document):
name = StringField()
address = ReferenceField(Address)
User.register_delete_rule(Address, 'owner', CASCADE)
# address.py
from mongoengine import *
class Address(Document):
owner = ReferenceField('User')